Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnEdit_Click(object sender, EventArgs e)
{
   sqlCon.Open();
   SqlCommand cmd = new SqlCommand("Update ItemInfo set Item_Name=@Item_Name, Dosage=@Dosage, Generic_Name=@Generic_Name where (Item_Name=@Item_Name)", sqlCon);
   cmd.Parameters.AddWithValue("@Item_Name", txtItemDescription.Text);
   cmd.Parameters.AddWithValue("@Dosage", txtDosage.Text);
   cmd.Parameters.AddWithValue("@Generic_Name", txtGeneric.Text);
   cmd.Parameters.AddWithValue("@Supplier_Name", comboSupplier.Text);
   cmd.ExecuteNonQuery();
   sqlCon.Close();
   MessageBox.Show("Successfully Updated", "Completed !", MessageBoxButtons.OK);
}
Posted
Updated 10-Apr-14 21:37pm
v2

1 solution

You have not used
@Supplier_Name

in your sql query, could one of the item_name supposed to be supplier_name?

+++++++++++++++++++++++++++++++++++++++
[round 2]
So the @Supplier_Name has not effect here, why add it as parameter?
You sql query is saying that "Look for those records whose item_name=@item_name, if found update their item_name to @item_name", did you notice something amiss here?

++++++++++++++++++++++++++++++++++++++++++
[round 3]
When you do update, you must have the Where clause to indicate which existing records to update, apparently, it cannot be item_name, because the new value (@item_name) that you give have not existed in the table yet. I think you are trying to change the name of an existing item, right? Does the item has an id? if so, use it in the where clause.
 
Share this answer
 
v4
Comments
Peter Leow 11-Apr-14 3:28am    
So the @Supplier_Name has not effect here, why add it as parameter?
You sql query is saying that "Look for those records whose item_name=@item_name and update their item_name to @item_name", did you notice something amiss here?
Member 10717094 11-Apr-14 3:29am    
sqlCon.Open();
SqlCommand cmd = new SqlCommand("Update ItemInfo set Item_Name=@Item_Name, Dosage=@Dosage, Generic_Name=@Generic_Name, Supplier_Name=@Supplier_Name where (Item_Name=@Item_Name)", sqlCon);
cmd.Parameters.AddWithValue("@Item_Name", txtItemDescription.Text);
cmd.Parameters.AddWithValue("@Dosage", txtDosage.Text);
cmd.Parameters.AddWithValue("@Generic_Name", txtGeneric.Text);
cmd.Parameters.AddWithValue("@Supplier_Name", comboSupplier.Text);
cmd.ExecuteNonQuery();
sqlCon.Close();
MessageBox.Show("Successfully Updated", "Completed !", MessageBoxButtons.OK);

message successfully updated. when the data on Item_Name is the one to update, no changes to database. other fields were perfectly updating except the one i put in the condition on sql query
Peter Leow 11-Apr-14 3:32am    
Of course, there is no error, because syntactically is correct, but logically is wrong, so there no update at all.
Member 10717094 11-Apr-14 4:05am    
Thanks Peter
Member 10717094 11-Apr-14 3:31am    
can you give me an alternate query?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900