Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Inserting integer and string into SQL Server database c#

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)
);
like image 326
mot375 Avatar asked Mar 13 '26 15:03

mot375


1 Answers

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;
like image 187
Steve Avatar answered Mar 16 '26 04:03

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!