ERROR ExecuteReader not initialized
Hello:
I am trying to read a record and getting an error: {"ExecuteReader: Connection property has not been initialized."}. Can someone help?
I have used the same connection inn other places and it does connect. I have the select statement displayed and it looks correct.
DBSQLSelect = "SELECT "
+ " [SEQNO]"
+ " ,[Name]"
+ " ,[Address_1]"
+ " ,[Address_2]"
+ " FROM"
+ " [" + Program_ID + "]"
+ " WHERE"
+ " [Company_ID] = '" + textBoxCompany_ID.Text + "'"
;
//
string DBConnection =
@"Server=" + Program.ProgramDBServer + ";"
+ "Database=" + Program.ProgramDBDatabase + ";"
+ "User ID=" + Program.ProgramDBLogin_ID + ";"
+ "Password=" + Program.ProgramDBPassword + ";";
//
MessageBox.Show("DBConnection=" + DBConnection);
MessageBox.Show("DBSQLSelect=" + DBSQLSelect);
//
SqlDataReader ReadRecord = null;
//
SqlConnection SQLConnect = new SqlConnection(DBConnection);
//
SqlCommand SQLCommand = new SqlCommand(DBSQLSelect);
//
SQLConnect.Open();
SQLConnect.GetSchema();
//
ReadRecord = SQLCommand.ExecuteReader(); //<<< {"ExecuteReader: Connection property has not been initialized."}
//
textBoxName.Text = (string) ReadRecord["Name"];
textBoxName.Text = ReadRecord.GetString(ReadRecord.GetOrdinal("Name"));
Sam HobbsPosted Mar 15, 2010, 10:31 PM
GustavoPosted Mar 15, 2010, 10:14 PM
I have changed the code to less lines for testing. I am still having the same error.
//---------------------------------------------------------
//using System;
//using System.Data;
//using System.Data.SqlClient;
string DBConnection =
@"Server=" + Program.ProgramDBServer + ";"
+ "Database=" + Program.ProgramDBDatabase + ";"
+ "User ID=" + Program.ProgramDBLogin_ID + ";"
+ "Password=" + Program.ProgramDBPassword + ";";
DBSQLSelect = "SELECT "
+ " [SEQNO]"
+ " ,[Name]"
+ " ,[Address_1]"
+ " ,[Address_2]"
+ " FROM"
+ " [" + Program_ID + "]"
+ " WHERE"
+ " [Company_ID] = '" + textBoxCompany_ID.Text + "'"
;
SqlDataReader ReadRecord = null;
SqlConnection MyConnection = new SqlConnection(DBConnection);
SqlCommand MyCommand = new SqlCommand(DBSQLSelect);
MyConnection.Open();
ReadRecord = MyCommand.ExecuteReader(); //<<< {"ExecuteReader: Connection property has not been initialized."}
textBoxName.Text = (string) ReadRecord["Name"];
//---------------------------------------------------------
theLizardPosted Mar 15, 2010, 9:51 PM
You have asked the same question in another thread.
I see you are trying to use what I posted by the simple fact that you have included GetSchema().
But are you using the class?