Click here to Skip to main content
15,885,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting below error when I run the code to update the table records.
I am able to achieve same with same update statement in different page but for some reason it gives error -
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '('.


Your help will be highly appreciated!

What I have tried:

ASP.NET
<pre>protected void SaveBtn_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("UPDATE project_table SET (project_language = @project_language, project_startdate = @project_startdate, project_enddate = @project_enddate, project_efforts = @project_efforts, project_status = @project_status) WHERE project_name =  '" +  DropDownList1.SelectedItem.Text + "' ", con);
            cmd.Parameters.AddWithValue("@project_language", LanguageDropDown.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@project_startdate", StartDateTxt.Text);
            cmd.Parameters.AddWithValue("@project_enddate", EndDateTxt.Text);
            cmd.Parameters.AddWithValue("@project_efforts", EffortsTxt.Text);
            cmd.Parameters.AddWithValue("@project_status", StatusDropDown.SelectedItem.Text);
            cmd.ExecuteNonQuery();
            
            Response.Write("<script>alert('Details Saved Successfully');</script>");
            LanguageDropDown.ClearSelection();
            EndDateTxt.Text = "";
            StatusDropDown.ClearSelection();
            StartDateCalendar.Visible = false;
            EffortsTxt.Text = "";
        }
Posted
Updated 16-Apr-22 22:23pm
Comments
DevilCow 16-Apr-22 19:42pm    
I think you need to put quotes around `project_language` (actually I think you need to quote all of them) at the beginning of the `UPDATE`
George Swan 17-Apr-22 2:19am    
There seems to be a missing double quotation mark surrounding the single quotation mark here:- WHERE project_name = '" +

There is no open bracket in an UPDATE command:
SQL
UPDATE MyTable SET MyColumn = @MyParameter, MyOtherColumn = @MyOtherParamater, ... WHERE ...

But since the rest of the command uses Parameter, the WHERE clause should also, or the whole operation is still open to SQL Injection.
 
Share this answer
 
ASP.NET
SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("update project_table Set project_language='" + LanguageDropDown.SelectedItem.Text + "',project_startdate='" + StartDateTxt.Text + "',project_enddate='" + EndDateTxt.Text + "',project_efforts='"+ EffortsTxt.Text +"', project_status='"+ StatusDropDown.SelectedItem.Text +"' Where project_name='" + DropDownList1.SelectedItem.Text + "'", con);
            cmd.ExecuteNonQuery();


this really helped. Thank you for all the help :)
 
Share this answer
 
Comments
Richard MacCutchan 17-Apr-22 9:29am    
That code is wide open to SQL injection and the loss of your database. See bobby-tables.com: A guide to preventing SQL injection[^].

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