简体   繁体   中英

Access database error :“Syntax Error in UPDATE statement.” in c#

I have a datagridview. I fatch data from database and show in this gridview. But when I try to update it by clicking a button it shows an error.

            try
            {
                string update = "update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?', where Tank_Unit='?' and companyID='?'";
                OleDbCommand cmd = new OleDbCommand(update, con);
                cmd.Parameters.AddWithValue("@Item_Code", cbItemEdit.Text);
                cmd.Parameters.AddWithValue("@Opening_Bal", txtOpeningBalanceEdit.Text);
                cmd.Parameters.AddWithValue("@Tank_Description", txtTankDesEdit.Text);
                cmd.Parameters.AddWithValue("@Tank_Unit", txtTankUnitEdit.Text);
                cmd.Parameters.AddWithValue("@companyID", label1.Text);
                i = cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
                if (i != 0)
                {
                    pnlView.Visible = true;
                    pnlEdit.Visible = false;
                }
            }

You've got a stray comma in there:

update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?', <<< where Tank_Unit='?' and companyID='?'

Remove that and it should work fine:

update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?' where Tank_Unit='?' and companyID='?'

There is extra , in your query before where

replace your query like this

string update = "update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?'  where Tank_Unit='?' and companyID='?'";

You have a comma after the last field being updated causing error. Try changing the statement to

string update = "update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?' where Tank_Unit='?' and companyID='?'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM