ADO.NET : OleDbCommand Parameterized Query Sequence Rule

While working with MS Access database with OleDbCommand you might have faced issue that your parameterized update statement is not working. The most frustrating part is that it also does not throw any runtime error. So ideally you paralyzedJ.

 

Golden rule is that you need to maintain the exact sequence of parameters you have specified in the sql statement. So while adding the parameter value sequence is very important.

 

Command parameter collection addition sequence has to match with the sql statement sequence.

 

String sSQL = "UPDATE PHONEDB SET [NAMES] = @pNames WHERE [ID] = @pID";

OleDbConnection conn = new OleDbConnection("ConnectionString");

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = conn;

cmd.CommandType = CommandType.Text;

cmd.CommandText = sb.ToString();

cmd.Parameters.AddWithValue("@pNames", "Your Name");

cmd.Parameters.AddWithValue("@pID", 12345);

 

 

Now if you alter the sequence like,

 

cmd.Parameters.AddWithValue("@pID", 12345);

cmd.Parameters.AddWithValue("@pNames", "Your Name");

 

Things would not work as expected.

 

Namoskar!!!