0

I am currently trying to connect to a database made in MS SQL Server and insert a row when a button on a form is clicked. Below is my code and an explanation of what is going wrong.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = 'u:\\my documents\\visual studio 2015\\Projects\\WindowsFormsApplication10\\WindowsFormsApplication10\\InventoryDB.mdf'; Integrated Security = True; Connect Timeout = 30");
            SqlCommand myCommand = new SqlCommand("INSERT INTO Table (Barcode, Item, Quantity) Values (123, 'Item 123', 5)", myConnection);
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
}

I believe I have connected to the database, because in previous attempts I was getting an error at myConnection.Open(). Now I am getting an error at myCommand.ExecuteNonQuery(); which says:

SqlException was Unhandled: An Unhandled Exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near the keyword 'Table'.

4
  • Your table does not exist! That's what the error is about... Do you have a table in SQL server that its actually named [Table], or is it a bad copy&paste? Commented Jun 9, 2015 at 15:29
  • 4
    @CrazyMenConnected: no, that's not the issue. The error says it all: Table is a keyword. msdn.microsoft.com/en-us/library/ms189822.aspx You can name your tables Table, but you shouldn't. Then you always have to use brackets to escape it: [Table]. Commented Jun 9, 2015 at 15:30
  • 1
    I can see a few things wrong here. The first and one that stands out the most is that your table is a reserved word, and as @squillman pointed out you can get around by bracketing the table name. You've got a couple of other potential problems in your code. You should be wrap your query in using blocks to ensure that the connections are properly closed and resources released, and the big one is you're not parameterizing your actual query Commented Jun 9, 2015 at 15:36
  • 1
    A word to the wise: though it's not necessary, in C# you can make it so you don't have to escape special characters in string literals by putting @ before the string. ie: SqlConnection myConnection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = 'u:\my documents\visual studio 2015\Projects\WindowsFormsApplication10\WindowsFormsApplication10\InventoryDB.mdf'; Integrated Security = True; Connect Timeout = 30"); Commented Jun 9, 2015 at 15:48

1 Answer 1

4

You can't use Table as the name for your table without qualifying it, since it's a SQL reserved word. Use [Table] instead if that is really your table name.

INSERT INTO [Table] (Barcode, Item, Quantity)...

Better yet, change the name of your table. Unless this is a copy/paste fail.

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

3 Comments

Thanks for your response. The name of my table really is "Table". I tried putting the bars around it, and even changed the name. I am receiving the same error, any other thoughts?
Can you post what your full INSERT statement looks like now?
The brackets fixed the problem, I just didn't update the table. Sorry about that, and thanks for your help!

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.