6

Hello I'm trying to figure out why i have this error

Incorrect syntax near the keyword 'Table'.

Thx in advance

Code :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace CSGObetsAdvisor
{
    public class SQLInsertData
    {
        public void InsertToSQL(string LoungeItemName)
        {
            string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + @"C:\Users\HP\documents\visual studio 2013\Projects\CSGObetsAdvisor\CSGObetsAdvisor\App_Data\Database.mdf" + ";Integrated Security=True";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO Table (ItemID,ItemName) VALUES (@ItemIDss,@Namess)");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("@ItemIDss", 50);
                cmd.Parameters.AddWithValue("@Namess", LoungeItemName);

                connection.Open();
                cmd.ExecuteNonQuery();
            }

        }


    }
}

Server Explorer :

enter image description here

Bart
  • 259
  • 2
  • 4
  • 17
  • 8
    `table` is a reserved work in SQL, making it a really bad name for a table. It should be surrounded by square braces in you insists on using it `insert into [table]`. I vote to close these types of questions as typographical errors. – Gordon Linoff Oct 19 '14 at 12:56
  • thx, I create new Table , it works now . – Bart Oct 19 '14 at 13:09

1 Answers1

8

That happens because TABLE is a reserved keyword for T-SQL.
If you really need to use that name, your query should enclose TABLE in square brackets

SqlCommand cmd = new SqlCommand(@"INSERT INTO [Table] 
                                  (ItemID,ItemName) VALUES 
                                  (@ItemIDss,@Namess)");

I strongly suggest to change that name and use a more descriptive word for the content of that table

Steve
  • 213,761
  • 22
  • 232
  • 286