Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, June 29, 2019 7:36 PM
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'.'"
<pre><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.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)
{
}
}
}
</code></pre>
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Incorrect syntax near the keyword 'Table'."
All replies (3)
Sunday, June 30, 2019 4:22 PM
Since “Table” is a special word in SQL, try using “[ ]”:
* cmd.CommandText = "insert into [Table] values . . .*
By the way, there is an advantageous alternative — Parameterised Queries.
Sunday, June 30, 2019 8:15 PM
Pretty sure the table in the video is named Table1.
Monday, July 1, 2019 8:46 AM
Hi Lerroy,
Sorry to trouble you and just want to confirm that if your issue is solved or not, if not, please feel free to let us know, we will try our best to help you out.
BTW, if you think the reply is helpful and could please mark it as answer, that benefits for other community members who meet the similar issue, thanks in advance.
If you have any other VS IDE issues, please let us know.
Have a nice day!
Best Regards,
Dylan
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]