Lerroy Fernandes

Lerroy Fernandes

  • NA
  • 17
  • 10.2k

Error : Incorrect syntax near the keyword 'Table'." ?

Jun 29 2019 2:42 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'."

Answers (5)