0

Can anyone tell me why when I try to add a field into this SQL Server database I get the error

Incorrect Syntax near the keyword 'Table'.

Code:

{
    // TODO: This line of code loads data into the 'tBoxDataSet.Table' table. You can move, or remove it, as needed.
    this.tableTableAdapter.Fill(this.tBoxDataSet.Table);
}

private void button1_Click(object sender, EventArgs e)
{
         SqlConnection cn = new SqlConnection(global::TicketBox.Properties.Settings.Default.TBoxConnectionString);
        try
        {
            using (SqlConnection connect = new SqlConnection(global::TicketBox.Properties.Settings.Default.TBoxConnectionString))
            using (SqlCommand runsql = new SqlCommand(@"INSERT INTO Table (Event_ID,Artist,Venue,Date,Time,Tickets) values(@EventID,@Artist, @Venue, @Date, @Time,@Tickets)", connect))
            {
                runsql.Parameters.Add("@EventID", SqlDbType.Int).Value = textBox1.Text;
                runsql.Parameters.Add("@Artist", SqlDbType.VarChar).Value = textBox2.Text;
                runsql.Parameters.Add("@Venue", SqlDbType.VarChar).Value = textBox3.Text;
                runsql.Parameters.Add("@Date", SqlDbType.Date).Value = textBox4.Text;
                runsql.Parameters.Add("@Time", SqlDbType.Time).Value = textBox5.Text;
                runsql.Parameters.Add("@Tickets", SqlDbType.Int).Value = textBox6.Text;
                connect.Open();
                runsql.ExecuteNonQuery();
            }

            MessageBox.Show("Event Added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.tableTableAdapter.Fill(this.tBoxDataSet.Table);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            cn.Close();
        }
1
  • 1
    I think 'Table' is a keyword. do you actualy have a table called 'Table'? I suggest you rename it to 'Concerts' or something Commented Mar 17, 2015 at 15:59

3 Answers 3

5

Table and Time are reserved keyword in T-SQL. You need to use them with square brackets like [Table] and [Time].

As a best practice, change them to non-reserved words.

Also Date can be a reserved keyword in future releases of SQL Server. You might need to use it with square brackets as well.

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

3 Comments

I cant believe I forgot that, been looking at this too long. Thanks fixed :)
Databases can be case sensitive; it the OP calls the table Table, it should be [Table] - not [TABLE], which can be an entirely different table ;p
@MarcGravell Exactly. Even their case are different, both Table and TABLE are behaves as reserved word. But yes, I edited to based on OP's original column names. Thanks.
2

if your table name is TABLE, then.. that is a keyword for sql...

change your query to

INSERT INTO [Table]

using [ ] will override the keyword

Comments

0

as Tabel is a reserved keyword , you need to use like this [table]

change the query to.

INSERT INTO [Table] (Event_ID,Artist,Venue,Date,Time,Tickets)  
values(@EventID,@Artist, @Venue, @Date, @Time,@Tickets)

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.