How to insert data into the sql server database

advertisements

I'm trying to insert data into my SQL Server 2014 database, but I get an error

Incorrect syntax near ')'.

My table matches the types of data I put in, for example, I'm put an int into a int.

Here is my code:

string ip = Request.UserHostAddress;
string name = "Jesus said: Love your Enemies (V.S.)";

int blueq = Convert.ToInt32(TextBox1.Text);
int redq = Convert.ToInt32(TextBox2.Text);
int whiteq = Convert.ToInt32(TextBox3.Text);
int blackq = Convert.ToInt32(TextBox4.Text);
int whiteqr = Convert.ToInt32(TextBox9.Text);
int redqr = Convert.ToInt32(TextBox10.Text);
int sn = 600;
int price = total_jslye;

string size; 

if (RadioButton1.Checked == false)
{
    size = "11x35";
}
else
    size = "18x50";

try
{
    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLCS"].ConnectionString;
    var cmd = "INSERT INTO cartsigns (@SignNumber, @redquantity, @bluequantity, @whitequantity, @blackquantity, @whitereflectivequantity, @redreflectivequantity, @size, @SignName, @ipaddress, @price)";

    using (SqlConnection com = new SqlConnection(conn))
    {
        using (SqlCommand cmds = new SqlCommand(cmd, com))
        {
            cmds.Parameters.AddWithValue("@SignNumber", sn);
            cmds.Parameters.AddWithValue("@redquantity", redq);
            cmds.Parameters.AddWithValue("@bluequantity", blueq);
            cmds.Parameters.AddWithValue("@whitequantity", whiteq);
            cmds.Parameters.AddWithValue("@blackquantity", blackq);
            cmds.Parameters.AddWithValue("@whitereflectivequantity", whiteqr);
            cmds.Parameters.AddWithValue("@redreflectivequantity", redqr);
            cmds.Parameters.AddWithValue("@size", size);
            cmds.Parameters.AddWithValue("@SignName", name);
            cmds.Parameters.AddWithValue("@ipaddress", ip);
            cmds.Parameters.AddWithValue("@price", price);

            com.Open();
            cmds.ExecuteNonQuery();
        }
    }
}

So please help, thanks


Your parameter names can not have brackets or spaces in them in SQL Server. So rename them all to @SignNumber, @redquantity, @bluequantity... etc.