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