nico chua nico chua -4 years ago 118
C# Question

C# update problems

I got this sqlException :


Incorrect syntax near ','.


newConnection.ConnectionM();
SqlCommand cmd = SqlConnectionOLTP.cn.CreateCommand();
cmd.CommandText = "Update Cost SET ActualAmount=@actualamount,FlagAdjustment=@flag, Date=@date where ProjectID=@projectid, PropertyID=@propertyid, CostCategoryID=@costcategoryid";
cmd.Parameters.AddWithValue("@actualamount", textBoxAmount.Text);
cmd.Parameters.AddWithValue("@flag", flag);
cmd.Parameters.AddWithValue("@date", dateTimePickerDate.Text);
cmd.Parameters.AddWithValue("@projectid", ProjectID);
cmd.Parameters.AddWithValue("@propertyid", PropertyID);
cmd.Parameters.AddWithValue("@costcategoryid", CostCatID);
cmd.ExecuteNonQuery();
SqlConnectionOLTP.cn.Close();
MessageBox.Show("Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.None);

Answer Source

The commas in where condition should be separated with and or OR or whatever condition you want to apply.

I have used this with and condition as below:

 newConnection.ConnectionM();
                SqlCommand cmd = SqlConnectionOLTP.cn.CreateCommand();
                cmd.CommandText = "Update Cost SET ActualAmount=@actualamount,FlagAdjustment=@flag, Date=@date where ProjectID=@projectid and PropertyID=@propertyid and CostCategoryID=@costcategoryid";
                cmd.Parameters.AddWithValue("@actualamount", textBoxAmount.Text);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download