0
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 '?'."

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Geekitygeek
1

Use named-parameters

cmd = new SqlCommand("insert into Insert1(sname,srno,sdob,sadd,sgender) values (@sname,@srno,@sdob,@sadd,@sgender)", cn);

cmdForm.Parameters.Add("@sname", OleDbType.Char).Value = this.txtname.Text;

cmdForm.Parameters.Add("@srno", OleDbType.Char).Value = this.txtrno.Text;

cmdForm.Parameters.Add("@sdob", OleDbType.Date).Value = this.dob.Value

cmdForm.Parameters.Add("@sadd", OleDbType.Char).Value = this.txtadd.Text;

cmdForm.Parameters.Add("@sgender", OleDbType.Char).Value = this.cmdgender.Text;
Votes + Comments
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.