-2

Im creating an application for my brother that will help him at work, I have created a C# form which should pass the data onto a sql table but i receive the error.

Dont know what to do further.

The code that im using is from the video https://www.youtube.com/watch?v=TIAOr2S6-SY&feature=youtu.be

I tried other methods that didnt help me to understand to no avail. Im trying to get into coding and would appreciate help.

"System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'Table'.'"



    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.IO;
    using System.Data.SqlClient;

    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            SqlConnection con=new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\lerro\OneDrive\Documents\DatabaseAmal.mdf;Integrated Security=True;Connect Timeout=30");

            public SqlConnection Con { get => con; set => con = value; }

            public Form1()
            {
                InitializeComponent();
            }

            private void label1_Click(object sender, EventArgs e)
            {

            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void label5_Click(object sender, EventArgs e)
            {

            }

            private void label9_Click(object sender, EventArgs e)
            {

            }

            private void button4_Click(object sender, EventArgs e)
            {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into Table values ('" +textBox1.Text+ "','" +textBox5.Text+ "','" + textBox2.Text + "','" +textBox3.Text + "','" +textBox4.Text + "','" +textBox6.Text + "','" +textBox4.Text + "','" +textBox7.Text + "','" +comboBox1.Text + "','" +textBox8.Text + "','" +textBox9.Text + "','" +dateTimePicker2.Text + "','" +textBox10.Text + "','" +comboBox2.Text + "','" +comboBox3.Text + "','" +comboBox4.Text + "','" +textBox14.Text + "','" +textBox11.Text + "','" +textBox12.Text + "','" +textBox13.Text + "','" + BrowseTextbox.Text + "','" + pictureBox1.Text + "','" + textBox15.Text + "','" +dateTimePicker1.Text + "')";
            cmd.ExecuteNonQuery();
            con.Close();

                MessageBox.Show("record inserted successfully");

            }

         private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*jpg; *.jpeg; *.gif; *.bmp;)|*jpg; *.jpeg; *.gif; *.bmp;";
            if (open.ShowDialog() == DialogResult.OK)
            {
                BrowseTextbox.Text = open.FileName;
                pictureBox1.Image = new Bitmap(open.FileName);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            textBox5.Text = String.Empty;
            textBox6.Text = String.Empty;
            textBox7.Text = String.Empty;
            textBox8.Text = String.Empty;
            textBox9.Text = String.Empty;
            textBox10.Text = String.Empty;
            textBox11.Text = String.Empty;
            textBox12.Text = String.Empty;
            textBox13.Text = String.Empty;
            textBox14.Text = String.Empty;
            textBox15.Text = String.Empty;
            pictureBox1.Image = null;
            comboBox1.Text = String.Empty;
            comboBox2.Text = String.Empty;
            comboBox3.Text = String.Empty;
            comboBox4.Text = String.Empty;
            BrowseTextbox.Text = String.Empty;






        }

            private void comboBox2_SelectedIndexChanged(object sender, 
    EventArgs e)
            {

            }
        }
    }

    

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Incorrect syntax near the keyword 'Table'."

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
  • I would suggest having a read of a few pages from [this google search](https://www.google.co.uk/search?q=how+to+write+mysql+insert+query) and then look in to using [Parameters](https://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp) before going too far in to your app – JayV Jun 29 '19 at 20:16
  • Using parameters will save you an obscene amount of headaches in the future and also make it easier for you to maintain/understand what is going on – JayV Jun 29 '19 at 20:17
  • What's the name table in the database? – Chetan Jun 29 '19 at 20:23
  • dbo.Table[Data] is the name @chetan Ranpariya – Lerroy Fernandes Jun 29 '19 at 20:26
  • You are not using that table name in the insert query. – Chetan Jun 29 '19 at 20:28
  • @ChetanRanpariya what should i do ? if you dont mind can you please tell me what needs to be done. Im a non coder just getting into it. xD – Lerroy Fernandes Jun 29 '19 at 20:31
  • You need to use `Table[Data]` in insert query. `insert into Table[Data] ....` – Chetan Jun 29 '19 at 20:40
  • Tried what nbk commented with and got the error 'An explicit value for the identity column in table 'Table' can only be specified when a column list is used and IDENTITY_INSERT is ON.' – Lerroy Fernandes Jun 29 '19 at 20:57
  • The table you are trying to insert data into has a column which has an auto generated value. Your query is trying to add a value to this column which is the issue. When writing SQL Queries it is often helpful and good practice to specify the columns you are inserting into, that way you can spot issues like this more easily. To do this after "insert into [tablename] " provide ([columnname1], [columnname2]) values ( .... etc – MJJames Jun 29 '19 at 21:15
  • As JayV and others has mentioned reading about Parameterized Queries now will really help, https://visualstudiomagazine.com/articles/2017/07/01/parameterized-queries.aspx would be a good starting point – MJJames Jun 29 '19 at 21:17
  • @MJJames im can share the solution files with you could you please help me with data updation in the database? Pretty please ! do comment if you can help me with that. ! Please i need help asap. Got a 20 hour deadline. – Lerroy Fernandes Jun 29 '19 at 21:46

1 Answers1

0

As you can see in the video he calls his table in the testing database

Table1

but in your Insert statement you write your table is

table

If you followed the video the must stay

 cmd.CommandText = "insert into Table1 values

or else some name you have choosen instead of Table1

nbk
  • 45,398
  • 8
  • 30
  • 47
  • but my table name is dbo.Table[Data] and i have kept the same name as Table. – Lerroy Fernandes Jun 29 '19 at 20:38
  • Try using `[Table]`, the word `Table` is probably a reserved keyword. – Lasse V. Karlsen Jun 29 '19 at 20:41
  • Done got this error now System.Data.SqlClient.SqlException: 'An explicit value for the identity column in table 'Table' can only be specified when a column list is used and IDENTITY_INSERT is ON.' – Lerroy Fernandes Jun 29 '19 at 20:47
  • please rebuild your database exactly in sqlfiddle and insert some data. next step is to debug your code and stop in the line after cmd.CommandText = "insert into Table1 values and then you also copy the Commandtext in your original post. – nbk Jun 29 '19 at 21:36
  • @nbk but i need to input the data that would be filled in the form, SQL fiddler shows hardcoded input. I dont have to worry about sql injections as the use would only be for the inhouse staff. – Lerroy Fernandes Jun 29 '19 at 22:00
  • Yes but you habe to check always, if your queries are valid. In your case use the insert statement that you get fro, your programm and test it in your database or in sqlfullde, sothat also over people can helps, everything else is guessing.on our oart because without knowing what your database design is and what you send to the sql server nobody can help. – nbk Jun 29 '19 at 22:23