1

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

7
  • good post, for best results with your questions, make sure you clarify your question in the body of your post along with adding a few examples of things you tried. You can always update your question. Welcome to stack overflow, recommended reading: stackoverflow.com/help/how-to-ask Commented May 14, 2015 at 3:52
  • I never tried to do it, but I think you cannot declare parameters with [...], try to remove spaces and declare them like @SignNumber without spaces Commented May 14, 2015 at 3:55
  • Why don't you do it the standard way? Check this and read the example at the end. Commented May 14, 2015 at 3:58
  • See the rules for naming identifiers here. Commented May 14, 2015 at 3:59
  • 1
    "I mean ... really?!" :-/ This is quite-obviously nothing more than "a case where a language-compiler has fallen off-the-bus." You made a syntax-error. The compiler detected the error shortly after it parsed the token int, and as a result it threw a "throw-away error message" (which actually makes no sense at all). Compilers do such things all the time. I can spot the syntax-error from here. . . Commented May 14, 2015 at 4:02

3 Answers 3

5

Your insert syntax is not correct. you have not given column names also keyword "Values" is missing in your query

Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

OK that helped, I got rid of all spaces and brackets in both the DB and my website. But it has the same error but says ")" instead of "int". Any Ideas?
I think you should update your question with the current code. Have you triple-checked all the parenthesis?
you are missing the keyword values in your insert statment. var cmd = "INSERT INTO cartsigns VALUES (@SignNumber,@redquantity,@bluequantity,@whitequantity,@blackquantity,@whitereflectivequantity,@redreflectivequantity,@size,@SignName,@ipaddress,@price)";
2

Try this code. you will get your desired result. The issue that you were getting in your code is because parameters are not supposed to have any spaces or brackets or any characters that makes the parameter names not well formed. A parameter name must begin with "@" character, and should follow the rules for object identifiers. Check the link for further details.

    string ip = Request.UserHostAddress;
    string name = "first sign";
    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 ([Sign Number],[red quantity],[blue quantity],[white quantity],[black quantity],[white reflective quantity],[red reflective quantity],[size],[Sign Name],[ipaddress],[price]) values (@[Sign_Number],@[red_quantity],@[blue_quantity], @[white_quantity],@[black_quantity],@[white_reflective_quantity],@[red_reflective_quantity],@[size],@[Sign_Name],@[ipaddress],@[price])";

            using (SqlConnection com = new SqlConnection(conn))
            {
                using (SqlCommand cmds = new SqlCommand(cmd, com))
                {
                    cmds.Parameters.AddWithValue("@[Sign_Number]", sn);
                    cmds.Parameters.AddWithValue("@[red_quantity]", redq);
                    cmds.Parameters.AddWithValue("@[blue_quantity]", blueq);
                    cmds.Parameters.AddWithValue("@[white_quantity]", whiteq);
                    cmds.Parameters.AddWithValue("@[black_quantity]", blackq);
                    cmds.Parameters.AddWithValue("@[white_reflective_quantity]", whiteqr);
                    cmds.Parameters.AddWithValue("@[red_reflective_quantity]", redqr);
                    cmds.Parameters.AddWithValue("@[size]", size);
                    cmds.Parameters.AddWithValue("@[Sign_Name]", name);
                    cmds.Parameters.AddWithValue("@[ipaddress]", ip);
                    cmds.Parameters.AddWithValue("@[price]", price);


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

                }
            }
        }
        catch
        {
            throw;
        }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.