0

I have written code in C# and integrated it with SQL Server, but each time I run it I keep getting this error:

System.Data.SqlClient.SqlException: 'Incorrect syntax near ';'.'

My code is this:

private void btnsubmit_Click(object sender, EventArgs e)
{
    var connectiontring =
        ConfigurationManager.ConnectionStrings["connData"].ConnectionString;

    string querystring = "INSERT INTO [Table] VALUES(@Name, @Gender, @Age, @Course, @Department, @Program, @Address, @PostCode, @Email;";

    using (var connection = new SqlConnection(connectiontring))
    {
        var cmd = new SqlCommand(querystring, connection);
        connection.Open();

        cmd.Parameters.AddWithValue("Name", tblName.Text);
        ...
        cmd.Parameters.AddWithValue("Email", tbllMail.Text);

        cmd.ExecuteReader();

        tblName.Text = "";
        ...
        tbllMail.Text = "";
    }
}
3
  • Please note that it's always recommended to include column names in insert statements: insert into [Table] (Name, Gender, Age...) VALUES (@Name, @Gender, @Age...). Also, SqlCommand is an IDisposable, and also, Can we stop using AddWithValue() already? Commented Feb 25, 2018 at 10:04
  • Haha.. What's with the AddwithValue? Commented Feb 26, 2018 at 4:14
  • That's a link, read the article... Commented Feb 26, 2018 at 4:54

2 Answers 2

3

SQL Server is right. Add a closing paren after the list of values:

string querystring = "insert into [Table] VALUES(@Name, ..., @Email);";
Sign up to request clarification or add additional context in comments.

Comments

2

You have a ; in the query string in place where ) should appear. Correct query string is:

 string querystring = 
    "insert into [Table] VALUES " +
    "   (@Name, @Gender, @Age, @Course, @Department, @Program, " +
    "    @Address, @PostCode, @Email)";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.