Hi,

I am a c# learner and I have got a problem - I have not much clue, please help. I have a windows form with two text boxes and a button called 'save'. If I click the save button - It should connect to the sql server express then create a data table called 'table', then create two collumns. After that it should put the value of textbox1 and textbox2 in column1 and column2 respectively. But I get some syntax error. Here are my codes.

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

namespace data_sql
{
    public partial class sql : Form
    {
        public sql()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            
            con.ConnectionString = "Data Source=(local);Initial Catalog = test; Trusted_Connection=True";
            cmd.Connection = con;
            con.Open();



            DataTable table = new DataTable("table");
            DataColumn clm;
            DataRow row;

            clm = new DataColumn();
            clm.DataType = System.Type.GetType("System.String");
            clm.ColumnName = "fsname";
            clm.ReadOnly = true;
            clm.Unique = true;
            table.Columns.Add(clm);

            clm = new DataColumn();
            clm.DataType = System.Type.GetType("System.String");
            clm.ColumnName = "ldname";
            clm.ReadOnly = true;
            clm.Unique = true;
            table.Columns.Add(clm);

            DataSet ds = new DataSet();
            ds.Tables.Add(table);

            cmd.CommandText = ("INSERT INTO table(fsname, ldname) VALUES (? , ?)");
            cmd.Parameters.AddWithValue("fsname", textBox1.Text);
            cmd.Parameters.AddWithValue("ldname", textBox2.Text);

            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Update is Successful");



        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            

        }
    }
}

I get 'Incorrect syntax near the keyword 'table'' error in cmd.ExecuteNonQuery();. I don't know what I am doing wrong here.

Thanks a lot in advance

>It should connect to the sql server express then create a data table called 'table', then create two collumns.

No need to create a DataTable.

private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            
            con.ConnectionString = "Data Source=(local);Initial Catalog = test; Trusted_Connection=True";
            cmd.Connection = con;
            con.Open();
 

            cmd.CommandText = "INSERT INTO table(fsname, ldname) VALUES (@fsname,@ldname)");
            cmd.Parameters.AddWithValue("@fsname", textBox1.Text);
            cmd.Parameters.AddWithValue("@ldname", textBox2.Text);

            cmd.ExecuteNonQuery();
            con.Close();

        }
Comments
Yes, he doesn't need to create it.

Thanks. I wanted to create a table programmaticcaly. But as you suggested, I created a table 'table' with column 'fsname' and 'ldname' from the sql server management studio. But NO LUCK. I get exactly the same error massage.

SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            
            con.ConnectionString = "Data Source=(local);Initial Catalog = test; Trusted_Connection=True";
            cmd.Connection = con;
            con.Open();
cmd.CommandText = ("INSERT INTO table(fsname, ldname) VALUES (@fsname,@ldname)");           
            cmd.Parameters.AddWithValue("@fsname", textBox1.Text);           
            cmd.Parameters.AddWithValue("@ldname", textBox2.Text); 
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Update is Successful");

Welcome to the Forums kibr987, Please take some time to introduce yourself at http://www.daniweb.com/forums/forum165.html

As for your solution:

Hover your mouse over the variable cmd, and expand the following as in the image to get the String being passed on please post this string.

Also post what Data type fsname and ldname have been declared as in the database.

Edited 6 Years Ago by finito: n/a

Attachments Screenshot-1.png 41.63 KB

Thanks finito...'ldname' and 'fsname' are nchar(10) datatype, declared in database.Also I am attaching the screen print - as you said.

Thanks in advance again

Hmm 2008 I can't be sure but have you added [] anywhere?

It seems like table doesn't exist Do you have a table called table?

Hmm 2008 I can't be sure but have you added [] anywhere?

Did you mean '("INSERT INTO table ([fsname], [ldname]) VALUES (@fsname,@ldname)");'.
There is a table called 'table'. Please find the screen print.

Did you mean '("INSERT INTO table ([fsname], [ldname]) VALUES (@fsname,@ldname)");'.

yes take out the [ ]

Thanks,

Still doesn't work. Exactly the same error.Grrr. I don't know what I am doing wrong

Bingoooo........I just have done it. It's the 'table'. It has to be '[table]'.

Thanks

Damn, I didn't know table was a reserved Name. But it makes sense.

Well I learned something thanks.

This article has been dead for over six months. Start a new discussion instead.