i have made a form in c# to add data into my datatable, it works fine, all i want is to return a message when the data is inserted, and i am having an issue with that, the code is as follows:
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = ("INSERT INTO datatable (Number_Plate,Registered_Keeper,Make,Model,Year_Of_Make,Colour,Engine_Size,Transmission,Fuel_Type) Values (@Number_Plate,@Registered_Keeper,@Make,@Model,@Year_Of_Make,@Colour,@Engine_Size,@Transmission,@Fuel_Type)");
cmd.Parameters.AddWithValue("@Number_Plate", Plate.Text);
cmd.Parameters.AddWithValue("@Registered_Keeper", Keeper.Text);
cmd.Parameters.AddWithValue("@Make", Make.Text);
cmd.Parameters.AddWithValue("@Model", Model.Text);
cmd.Parameters.AddWithValue("@Year_Of_Make", Year.Text);
cmd.Parameters.AddWithValue("@Colour", Colour.Text);
cmd.Parameters.AddWithValue("@Engine_Size", Engine.Text);
cmd.Parameters.AddWithValue("@Transmission", Transmission.Text);
cmd.Parameters.AddWithValue("@Fuel_Type", Fuel.Text);
SqlDataReader reader = cmd.ExecuteReader();
if (cmd.ExecuteNonQuery()==1)
{
button1.Visible = false;
label10.Visible = true;
}
else
{
button1.Visible = false;
label10.Text = "Data Not Added Please try Again!";
label10.Visible = true;
}
}
when i run the code, i get an issue with the If statement ane the error is:
There is already an open DataReader associated with this Command which must be closed first.
any help appreciated.
SqlDataReader reader = cmd.ExecuteReader();in there? That doesn't make any sense... and the error is telling you exactly that.usingstatement.SqlConnection,SqlCommand, andSqlDataReader(though you don't need it here) all implementIDisposable.