Reputation: 11
I keep getting the error
ExecuteReader: Connection Property has not been initialized
in C# and Visual Studio. I have tried some solutions that I researched but they do not seem to be doing the trick.
Here is my code:
public static int AddCustomer(Customer customer)
{
MySqlConnection connection = MySqlCommand.GetConnection();
string strInsertStatement =
"INSERT Customers (Name, Address, City, State, ZipCode) " +
"VALUES (@Name, @Address, @City, @State, @ZipCode)";
MySql.Data.MySqlClient.MySqlCommand insertCommand = new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement);
insertCommand.Parameters.AddWithValue("@Name", customer.strFirstName);
insertCommand.Parameters.AddWithValue("@Address", customer.strStreetName);
insertCommand.Parameters.AddWithValue("@City", customer.strCity);
insertCommand.Parameters.AddWithValue("@State", customer.strState);
insertCommand.Parameters.AddWithValue("@ZipCode", customer.strPhoneNumber);
try
{
connection.Open();
insertCommand.ExecuteNonQuery();
string strSelectStatement = "SELECT IDENT_CURRENT('Customers') FROM Customers";
MySql.Data.MySqlClient.MySqlCommand selectCommand = new MySql.Data.MySqlClient.MySqlCommand(strSelectStatement, connection);
int customerID = Convert.ToInt32(selectCommand.ExecuteScalar());
return customerID;
}
catch (MySqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
What am I missing that is making this error appear?
Upvotes: 0
Views: 226
Reputation: 424
In which line do you get the error? I just had a quick look at your code and think you forgot to add your connection to the insert command use the following instead:
EDIT: Put all the code here and added printing of error to console and assignment of connection.
I guess you get your error in the open() method call. There is no connect string in your solution. Don't forget to enter your credentials.
public static int AddCustomer(Customer customer)
{
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test;"
string strInsertStatement =
"INSERT Customers " +
"(Name, Address, City, State, ZipCode) " +
"VALUES (@Name, @Address, @City, @State, @ZipCode)";
MySql.Data.MySqlClient.MySqlCommand insertCommand =
new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement);
insertCommand.Parameters.AddWithValue(
"@Name", customer.strFirstName);
insertCommand.Parameters.AddWithValue(
"@Address", customer.strStreetName);
insertCommand.Parameters.AddWithValue(
"@City", customer.strCity);
insertCommand.Parameters.AddWithValue(
"@State", customer.strState);
insertCommand.Parameters.AddWithValue(
"@ZipCode", customer.strPhoneNumber);
try
{
connection.Open();
insertCommand.Connection = connection;
insertCommand.ExecuteNonQuery();
string strSelectStatement =
"SELECT IDENT_CURRENT('Customers') FROM Customers";
MySql.Data.MySqlClient.MySqlCommand selectCommand =
new MySql.Data.MySqlClient.MySqlCommand(strSelectStatement, connection);
int customerID = Convert.ToInt32(selectCommand.ExecuteScalar());
return customerID;
}
catch (MySqlException ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
finally
{
connection.Close();
}
}
Upvotes: 0
Reputation: 633
You need to set the Connection property of the command. Use the constructor that accepts a MySqlConnection.
MySql.Data.MySqlClient.MySqlCommand insertCommand = new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement, connection);
Upvotes: 0
Reputation: 57
You have to give the connection as second parameter to MySQlCommand:
MySql.Data.MySqlClient.MySqlCommand insertCommand =
new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement, connection);
Upvotes: 1
Reputation: 1619
The error says it all: Connection Property has not been set.
You have a Connection, and you have insertCommand. Nowhere in the code are you telling insertCommand to use the Connection.
insertCommand.Connection = connection;
Or, possibly, as part of its connector as you do with the Select.
Upvotes: 1