SqlConnection cn = new SqlConnection(MyConn.cnstring);
//string sql = "insert into Insert1 values(?,?,?,?,?)";
SqlCommand cmd ;
cmd = new SqlCommand("insert into Insert1 values(?,?,?,?,?)", cn);
cmd.Parameters.AddWithValue("sname", txtname.Text);
cmd.Parameters.AddWithValue("srno", txtrno.Text);
cmd.Parameters.AddWithValue("sdob", dob.Value);
cmd.Parameters.AddWithValue("sadd", txtadd.Text);
cmd.Parameters.AddWithValue("sgender", cmdgender.Text);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
When I use this code folowing error will gererate
"Incorrect syntax near '?'."
Loading
Amit ChoudharyPosted Mar 8, 2010, 6:41 AM
use following syntax to user parameterized query with command object:
you can user cmd.Parameters.AddWithValue(); method also but use @.
Please mark it as answer if it helps you.
PankaJ DPosted Mar 8, 2010, 5:46 AM
Hirendra SisodiyaPosted Mar 8, 2010, 4:55 AM
SqlConnection cn = new SqlConnection(MyConn.cnstring);
cn.Open();
SqlCommand cmd ;
cmd = new SqlCommand("insert into Insert1 values('"+ txtname.Text +"','"+ txtrno.Text +"','"+ dob.Text +"','"+ txtadd.Text +"','"+ cmdgender.Text +"',)", cn);
cmd.ExecuteNonQuery();
cn.Close();
thanks
Lalit MPosted Mar 8, 2010, 4:47 AM