2

I am having trouble inserting the integer value [Question Type] and the string [Question Space] into my database at the same time into the same row. Whenever I click the button and try to execute an error comes up saying:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near '('.

Code:

SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);

command5.ExecuteNonQuery();

I would appreciate any help that anyone can give me.

Here's the whole code for the button if you want to see:

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;

    SqlConnection connect = new SqlConnection(connectionString);
    connect.Open();

    int checkedradiobutton = 0;
        
    if(radioButton1.Checked)
    {
        checkedradiobutton = 1;
    }
    else if(radioButton2.Checked)
    {
        checkedradiobutton = 2;
    }
    else if(radioButton3.Checked)
    {
        checkedradiobutton = 3;
    }

    string QuestionText = QuestionBox.Text;

    SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect);
    command5.Parameters.AddWithValue("@QuestionText", QuestionText);
    command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);

    command5.ExecuteNonQuery();
}

And my database table:

CREATE TABLE [dbo].[Questions] 
(
     [QuestionID]     INT           IDENTITY (1, 1) NOT NULL,
     [Actual answer]  NVARCHAR (50) NULL,
     [Question Space] NVARCHAR (50) NULL,
     [Question Type]  INT           NULL,

     PRIMARY KEY CLUSTERED ([QuestionID] ASC)
);

3 Answers 3

6

The correct syntax for INSERT INTO is

INSERT INTO <table> (field1, field2, fieldN) VALUES (value1, value2, valueN)

so your command should be written as

SqlCommand command5 = new SqlCommand(@"INSERT INTO Questions  
                        ([Question Space], [Question Type]) VALUES  
                        (@QuestionText, @checkedradiobutton)", connect);

Note how the parameters placeholders should not be enclosed in single quotes because doing that you tranform them in literal text. (Inserts the "@QuestionText" string in your [Question Space] field)

Finally, try to avoid AddWithValue, it is a shortcut with numerous drawbacks as you can read in Can we stop using AddWithValue already and in How Data Access Code Affects Database Performance

This is still a single line of code

 command5.Parameters.Add("@QuestionText", SqlDbType.NVarChar).Value = QuestionText;
Sign up to request clarification or add additional context in comments.

1 Comment

@mot375: if you feel this answer helped you solve your problem, then please accept this answer. This will show your appreciation for the people who spent their own time to help you.
2

You don't have to wrap parameters with ' inside the query. Start cleaning it and see if it works

    SqlCommand command5 = new SqlCommand("INSERT INTO [dbo].[Questions] ([Question Space], [Question Type]) Questions VALUES (@QuestionText, @checkedradiobutton)", connect);
    command5.Parameters.AddWithValue("@QuestionText", QuestionText);
    command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);


    command5.ExecuteNonQuery();

Comments

1

The INSERT syntax is wrong, the name of the table should go after "INSERT INTO", e.g.:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Cheers.

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.