Skip to content
Advertisement

“Incorrect syntax near ‘)’.”

I have a program that is to take a couple inputs and do stuff to them then store them in a local database (using SQL Server).

I have the following code for the connection:

SqlConnection con = new SqlConnection();
con.ConnectionString = ("Data Source=DESKTOP-PGHMM6M;Initial Catalog=LocalUsers;Integrated Security=True");

con.Open();

String st = "INSERT INTO data(Username,Password, Hash, EncryptedPassword)";
SqlCommand cmd = new SqlCommand(st, con);

cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", textBox2.Text);         
cmd.Parameters.AddWithValue("@Hash", savedPasswordHash);
cmd.Parameters.AddWithValue("@EncryptedPassword", FinalEncryptedPass);

cmd.ExecuteNonQuery();

con.Close();

It fails at the cmd.ExecuteNonQuery(); line, and throws this exception :

Incorrect syntax near ‘)’

I’m not even really sure where to start, as I haven’t done any of this since college (107 years ago). Can anybody help me get started? I’ve been scouring but nothing seems to be working.

Advertisement

Answer

The VALUES clause will need to be within the insert statement string. I would also recommend a using block instead of directly opening the SqlConnection, as will automatically close the connection upon exit whether by completion or error. While technically there isn’t a difference between using String and string as in your command text, String would most often be used to reference a class while string is typically used in object references such as the case here.

string connectionString = @"YourConnectionString";

string st = "INSERT INTO data(Username,Password, Hash, EncryptedPassword) VALUES (@Username, @Password, @Hash, @EncryptedPassword)";

using (SqlConnection con = new SqlConnection(connectionString))
{
    SqlCommand cmd = new SqlCommand(st1, con);

    cmd.Parameters.AddWithValue("@Username", Username);
    cmd.Parameters.AddWithValue("@Password", textBox2.Text);
    cmd.Parameters.AddWithValue("@Hash", savedPasswordHash);
    cmd.Parameters.AddWithValue("@EncryptedPassword", FinalEncryptedPass);

    con.Open();
    cmd.ExecuteNonQuery();
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement