Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code on an update button in WinForms VS 2017.
C#
private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtOrder.Text != "")
            {
                cmd = new SqlCommand("UPDATE AER_INBOUND_PLASTICS SET [STATUS]=@status,[DATE]=@date,[SOPO_NO]=@order,[SHIPPER]=@shipper,[VENDOR]=@vendor,[WAREHOUSE]=@warehouse,[COMMENTS]=@comments,[PURCH_PART_CODE]=@part1,[PURCH_PART_CODE_2]=@part2,GETDATE(),[MODIFIED_BY]=@user WHERE ID=@id", con);
                con.Open();
                cmd.Parameters.AddWithValue("@id", ID);
                cmd.Parameters.AddWithValue("@status", comboBoxStatus.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@date", txtLoadtime.Text);
                cmd.Parameters.AddWithValue("@order", txtOrder.Text);
                cmd.Parameters.AddWithValue("@shipper", txtShipper.Text);
                cmd.Parameters.AddWithValue("@vendor", txtVendor.Text);
                cmd.Parameters.AddWithValue("@warehouse", comboBoxWarehouse.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@comments", txtComments.Text);
                cmd.Parameters.AddWithValue("@part1", txtPart1.Text);
                cmd.Parameters.AddWithValue("@part2", txtPart2.Text);
                cmd.Parameters.AddWithValue("@user", Environment.UserName);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Updated Successfully.");
                con.Close();
                dataGridView1.Refresh();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Select Record to Update.");
            }
        }
The form loads correctly, and the textboxes are populated correctly from the DataGridView SelectedRow. When I modify the textboxes and click on the update button, I get the following error on the cmd.ExecuteNonQuery() line: Additional information: Incorrect syntax near '('.

I can't see any parentheses missing anywhere. I can't seem to track down where the error is in the update process.

What I have tried:

I scoured the code and did find a square bracket missing in the SqlCommand statement, but fixing that has not resolved the problem. I am following the instructions on this site (http://www.c-sharpcorner.com/uploadfile/1e050f/insert-update-and-delete-record-in-datagridview-c-sharp/)
Posted
Updated 8-Mar-18 8:08am
v2

You have a random GETDATE() in your code that you are not assigning to any column.

Looks like you copied the UPDATE statement from an earlier INSERT one and forgot to put the column name in
 
Share this answer
 
Comments
Maciej Los 8-Mar-18 14:01pm    
Hawk eye!
CHill60 8-Mar-18 14:12pm    
Hi Maciej! It's been a while since you called me that :-)
Maciej Los 8-Mar-18 14:22pm    
Oh, yeah! You deserved for such of compliment, because you're very perceptive person, Caroline.
BTW: All the best of Women's Day!
CHill60 8-Mar-18 14:30pm    
Many thanks! I wore purple because apparently it was the thing to do :-)
Maciej Los 8-Mar-18 14:35pm    
;)
Oh for Pete's sake!! There is a field in the table called MODIFIED which is the datetime the record is modified. I put in [MODIFIED]=GETDATE() and it works perfectly! Thanks!!
 
Share this answer
 
Comments
Maciej Los 8-Mar-18 14:11pm    
This is not an answer. Please, delete it to avoid down-voting.
To say "Thank you" use "Have a Question or Comment" widget under the solution/answer.
CHill60 8-Mar-18 14:12pm    
My pleasure - Feel free to accept the solution. By the way, avoid posting comments as solutions - there is a "Have a Question or Comment?" link next to posts and a "Reply" link next to comments. I suggest you delete this before you get too many downvotes :-)

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