1

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

3
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by finito
Featured Replies
  • 1
    __avd 1,826   6 Years Ago

    [b]>It should connect to the sql server express then create a data table called 'table', then create two collumns.[/b] No need to create a DataTable. [code] private void btnSave_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); SqlCommand cmd = new SqlCommand(); con.ConnectionString = "Data Source=(local);Initial Catalog = test; … Read More

1

>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();

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

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");
0

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 by finito: n/a

Attachments Screenshot-1.png 41.63 KB
0

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

0

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?

0

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.

0

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

yes take out the [ ]

0

Thanks,

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

0

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

Thanks

0

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

Well I learned something thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.