0

I want to create a program that save/deletes/edits database files. For example I want to enter this info to the database:

Name: John 
Surname: Johnson 
Father's name: Johnson 
Offers used: 10 
Age: 20

And all this to be saved or edited or removed. But when I try to make this

  1. I can't change the database table name
  2. I get this error:

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

Here is my code:

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 Photography_Register
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Ivan\Documents\db.mdf;Integrated Security=True;Connect Timeout=30");

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();

            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into Table values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";

            cmd.ExecuteNonQuery();

            con.Close();           

            MessageBox.Show("Data about " + textBox1.Text + " " + textBox3.Text + " has been successfully" + "\r" + "uploaded to server!");
        }
    }
}
nkr
  • 3,026
  • 7
  • 31
  • 39

1 Answers1

3

Table is a reserved word for T-SQL. You need to write it between [ ] in case it is really the name of your table.

Like this.

insert into [Table] values ...

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-2017

Update with parameters for the second problem (incorrect number of columns…)

How do I to insert data into an SQL table using C# as well as implement an upload function?

SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO [Table] (ColumnName1, ColumnName2, ColumnName3, ColumnName4, ColumnName5) VALUES (@ColumnName1, @ColumnName2, @ColumnName3, @ColumnName4, @ColumnName5)";

cmd.Parameters.AddWithValue("@ColumnName1", textBox1.Text);
cmd.Parameters.AddWithValue("@ColumnName2", textBox2.Text);
cmd.Parameters.AddWithValue("@ColumnName3", textBox3.Text);
cmd.Parameters.AddWithValue("@ColumnName4", textBox4.Text);
cmd.Parameters.AddWithValue("@ColumnName5", textBox5.Text);

cmd.ExecuteNonQuery();

con.Close();

Just replace ColumnName1 ColumnName2... with the name of your columns and textBox1.Text, textBox2.Text... with your input textboxes

Don't forget to cast the textboxX.Text to the desired datatype if the column doesn't match with string type.

https://learn.microsoft.com/es-es/dotnet/api/system.int32.parse?view=netframework-4.7.2

cmd.Parameters.AddWithValue("@ColumnName4", int.Parse(textBox4.Text));
cmd.Parameters.AddWithValue("@ColumnName5", int.Parse(textBox5.Text));
blfuentes
  • 2,731
  • 5
  • 44
  • 72
  • 1
    Based on the data example in the question, columns 4 & 5 look like integers. – LarsTech Mar 11 '19 at 19:15
  • @LarsTech yes, that's why I updated my answer with the advice for a possible cast. – blfuentes Mar 11 '19 at 19:16
  • @blfuentes it doesnt give error now everything works fine but i cant see the info written in the dbo.Table Any suggestions about that? – Ivan Latunov Mar 11 '19 at 19:38
  • @IvanLatunov hard to figure out why… you could check the connection string of the command to verify the `mdf` file is the one you want. – blfuentes Mar 11 '19 at 19:43
  • I've uploaded pics from the dbo.Table and the code. Here are the links: https://pastebin.com/UMF21sfy https://www.photobox.co.uk/my/photo?album_id=5592143445&photo_id=501720668220 – Ivan Latunov Mar 11 '19 at 19:44
  • 1
    @blfuentes thank you very much!!!!! everything is working fine i've just didn't linked the database with the datagridview but now everything is working! thanks again!!! – Ivan Latunov Mar 11 '19 at 19:56
  • 1
    @IvanLatunov nice you fixed it. Don't forget to mark the answer as correct if it helped to resolve the issue – blfuentes Mar 11 '19 at 19:56