Thread: update info
View Single Post
  #1 (permalink)  
Old July 4th, 2009, 02:58 AM
Banishah Banishah is offline
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default update info

HI.....now i am trying to update the contact name and phone in the Customers table by not changing the customer ID(primary key)....so here is my code...look for the highlighted error()

protectedvoid Button1_Click(object sender, EventArgs e)
{
SqlConnection conCust;
SqlCommand cmdUpdate,cmdSelect;
SqlDataReader dtrCust;
string conStr = ConfigurationManager.ConnectionStrings["ConnectionCust"].ConnectionString;
conCust = newSqlConnection(conStr);

cmdSelect = newSqlCommand("Select Customerid From Customers",conCust);
cmdUpdate = newSqlCommand("Update Customers set ContactName=@contName,phone=@con_phone Where CustomerID=@id)",conCust);
conCust.Open();
dtrCust = cmdSelect.ExecuteReader();

if (dtrCust.HasRows)
{
cmdUpdate.Parameters.AddWithValue("@contName", TextBox2.Text);
cmdUpdate.Parameters.AddWithValue("@con_phone", TextBox3.Text);

}
else
{
Label1.Text = "Customer ID cannot be updated...</br> Enter id that already exist";
}
cmdUpdate.ExecuteNonQuery();//error appear here(invalidOperationException)


conCust.Close();
Label1.Text = "Record is updated";

}
it says... there is already an open DataReader associated with this Command which must be closed first.....how am i going to close it?and i want to clarify something if i want to update the info in the table without changing the customer ID....i must read(sqlDataReader) it right?
Reply With Quote