Ahmad Saidi Ahmad Saidi - 24 days ago 6
C# Question

error Incorrect syntax near in c#

This statement gives the following error:


An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near ','.


Note: My table has 22 fields
thanks for your help

con.ConnectionString = @"Data Source =.\MYSQL; Initial Catalog = db_info; Integrated Security = True";
con.Open();
/SqlCommand com = new SqlCommand("insert into tbl_tarifmoshtari values(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16,,,,,,,)", con);
SqlCommand com1 = new SqlCommand();

com1.Parameters.AddWithValue("@p1", Convert.ToInt32(txbox_shomaremoshtari.Text));
com1.Parameters.AddWithValue("@p2", cmbo_jensiyat.Text);
com1.Parameters.AddWithValue("@p3", txbox_nam.Text);
com1.Parameters.AddWithValue("@p4", txbox_famil.Text);
com1.Parameters.AddWithValue("@p5", txbox_pedar.Text);
com1.Parameters.AddWithValue("@p6", txbox_tarikhtavalod.Text);
com1.Parameters.AddWithValue("@p7", txbox_shomareshenasname.Text);
com1.Parameters.AddWithValue("@p8", txbox_codmeli.Text);
com1.Parameters.AddWithValue("@p9", txbox_mahaltavalod.Text);
com1.Parameters.AddWithValue("@p10", txbox_serishenasname.Text);
com1.Parameters.AddWithValue("@p11", txbox_serialshenasname.Text);
com1.Parameters.AddWithValue("@p12", txbox_adresmanzel.Text);
com1.Parameters.AddWithValue("@p13", txbox_codpostimanzel.Text);
com1.Parameters.AddWithValue("@p14", Convert.ToInt32(txbox_telephonmanzel.Text));
com1.Parameters.AddWithValue("@p15", Convert.ToInt32(txbox_mobil.Text));
com1.Parameters.AddWithValue("@p16", txbox_email.Text);
com1.ExecuteNonQuery();

Answer

If your table has 23 columns and you dont want to insert into all 23 columns then mention the required column list in Insert. No need to add commas

Insert into tbl_tarifmoshtari 
(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16)
values
(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16)