Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
With this codes I m still getting error

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

Additional information: Incorrect syntax near ')'.

C#
<pre lang="text"> private void insert_Click(object sender, RoutedEventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand (@"Insert into mob (FirstName, LastName, Mobile, emailid, Category) VALUES 
                ('"+ First.Text + "','" + Last.Text +"','" + Mobile.Text +"', '" + email.Text + "','" +comboBox + "',)", con);
            cmd.ExecuteNonQuery();
            con.Close();


        } 
Posted
Updated 1-Dec-15 18:21pm
v2
Comments
jgakenhe 2-Dec-15 0:35am    
Suvendu took out the comma before the ); so it should work. Though you should not put this on the open Internet because of the SQL Injection vulnerability.

Anything the user types in will be executed by the SQL Server; so if they type DELETE in first name it will get executed by SQL Server.

1 solution

You have an additional comma (,) at the end of the list of values in the INSERT statement.
Try this-
C#
SqlCommand cmd = new SqlCommand (@"Insert into mob (FirstName, LastName, Mobile, emailid, Category) VALUES
              ('"+ First.Text + "','" + Last.Text +"','" + Mobile.Text +"', '" + email.Text + "','" +comboBox + "')", con);

Note: Your code is vulnerable to SQL Injection. I strongly recommend to use Parameterized Query or Stored Procedure instead.
Reference:
Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

Hope, it helps :)
 
Share this answer
 
v2

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