Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near '='.


C#
private void buttonupdate_Click(object sender, EventArgs e)
        {
            connection.Open();
            //SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            SqlCommand command = new SqlCommand("UPDATES entry SET  Name = @name, Father Name = @fathername, Gender = @gender, CNIC = @cnic, Mobile = @mobile, Date of Birth = @birthdate, Address = @address,Village Council =  @vc, Tehsil = @tehsil, District = @district, occupation = @occupation where Serial No = @serialno", connection);
            command.Parameters.Add(new SqlParameter("@serialno", textBoxserialno.Text));
            command.Parameters.Add(new SqlParameter("@name", textBoxname.Text));
            command.Parameters.Add(new SqlParameter("@fathername", textBoxfathername.Text));
            command.Parameters.Add(new SqlParameter("@gender", comboBoxgender.GetItemText(comboBoxgender.SelectedItem)));
            command.Parameters.Add(new SqlParameter("@cnic", textBoxcnic.Text));
            command.Parameters.Add(new SqlParameter("@mobile", textBoxmobile.Text));
            command.Parameters.Add(new SqlParameter("@birthdate", dateTimePickerdob.Value.ToString()));
            command.Parameters.Add(new SqlParameter("@address", textBoxvillage.Text));
            command.Parameters.Add(new SqlParameter("@vc", textBoxvc.Text));
            command.Parameters.Add(new SqlParameter("@tehsil", textBoxtehsil.Text));
            command.Parameters.Add(new SqlParameter("@district", textBoxdistrict.Text));
            command.Parameters.Add(new SqlParameter("@occupation", textBoxoccupation.Text));
            command.ExecuteNonQuery();
            connection.Close();
            MessageBox.Show(textBoxname.Text, "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
           

        }


What I have tried:

I have tried but not succeeded.
Posted
Updated 6-May-20 10:36am
v2
Comments
Maciej Los 6-May-20 16:13pm    
UPDATES??? It should be UPDATE!!!
AttaUrRahman 6-May-20 16:40pm    
Thanks.

Try this:
C#
SqlCommand command = new SqlCommand("UPDATE entry SET  Name = @name, [Father Name] = @fathername, Gender = @gender, CNIC = @cnic, Mobile = @mobile, [Date of Birth] = @birthdate, Address = @address, [Village Council] =  @vc, Tehsil = @tehsil, District = @district, occupation = @occupation
where [Serial No] = @serialno", connection);


Changes:
1. UPDATES replaced with UPDATE
2. Every column name with space between words has been arounded with []
 
Share this answer
 
v3
Comments
AttaUrRahman 6-May-20 16:42pm    
Thank You Sir.
Maciej Los 6-May-20 16:51pm    
You're very welcome.
MadMyche 6-May-20 19:13pm    
+5
Maciej Los 7-May-20 0:39am    
Thank you.
If you have access to an SQL Development program (such as Sql Server Management Studio) you should author your SQL Command there and try them out. And pay attention to the intellisense and highlighting that it applies.
SQL
UPDATES @Entry                      -- UPDATES is NOT highlighted

SET Name            = @names        -- Name is highlighted
,   Father Name     = @fathername   -- Name is highlighted
,   Gender          = @gender
,   CNIC            = @cnic
,   Mobile          = @mobile
,   Date of Birth   = @birthdate    -- Date is highlighted
,   Address         = @address      -- Address is highlighted
,   Village Council =  @vc
,   Tehsil          = @tehsil
,   District        = @district
,   occupation      = @occupation

where (Serial No = @serialno)      -- Both Serial and No are highlighted

1. It should be UPDATE as previously noted
2. Name is a "special" word
3. Space in column name, should be wrapped [Father Name]
4. Date is a data type, space in column name
5. Address is a "special" word
6. Both Serial and No are special words

My rules of development...
1. If a column name or variable changes colors when I type, I change that items name
2. No spaces in column names (or variables)
3. Use a SQL IDE when developing a query for writing and testing
 
Share this answer
 
Comments
Maciej Los 6-May-20 16:51pm    
5ed!
MadMyche 6-May-20 19:13pm    
Thank you
SqlCommand command = new SqlCommand("UPDATES entry SET Name = @name, Father Name = @fathername, Gender = @gender ...<br />
                            Try removing the space between 'Father' and 'Name'
 
Share this answer
 
v3
Comments
AttaUrRahman 6-May-20 16:40pm    
Thanks.
Does the field 'Father Name' have a space in it if so you need to put the field name in square brackets.
 
Share this answer
 

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