This function will display an error that “connection has not been properly initialized.” Why?
public void updateCustomer()
{
using (SqlCeConnection aConnection = new SqlCeConnection(@"Data Source=|DataDirectory|\Database.sdf"))
{
aConnection.Open();
using (SqlCeCommand aCommand = new SqlCeCommand("UPDATE customer SET credit = @credit WHERE(ID = @ID)"))
{
aCommand.Parameters.Add(new SqlCeParameter("credit", SqlDbType.NVarChar, 100)).Value = getRemaining();
aCommand.Parameters.Add(new SqlCeParameter("ID", SqlDbType.Int, 8)).Value = Convert.ToInt32(textBox2.Text);
int resultUpdate = aCommand.ExecuteNonQuery();
if (resultUpdate != -1)
label9.Text = "customer updated sussessfully";
else
label9.Text = "some error in updating customer";
aConnection.Close();
}
}
}
you need to set the connection for the SqlCeCommand
set connection string as
check Connection strings for SQL Server Compact Edition for more information