Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
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!!!

Posted: Thursday, January 24, 2008 9:22 PM by wriju
Filed under: ,

Comments

Noticias externas said:

While working with MS Access database with OleDbCommand you might have faced issue that your parameterized

# January 24, 2008 4:32 PM

jholland@sc.rr.com - John said:

I am trying to insert rows into an Access database using vb.net(2003).  I am having a problem setting up the command string, connection string, transaction code.  This should be farely easy but I cannot seem to be able to the right questions in Google.

# July 8, 2008 3:40 PM

wriju said:

# July 9, 2008 9:34 AM

Davy Wagenmakers said:

Great! Normally I use MS SQL and that can handle a different sequence. I have been very frustrated that it didn't work, couldn't figure out what the problem was.

You helped me a lot! Thnx!

# August 14, 2008 10:09 AM

ламинат said:

8mGood idea.3p I compleatly disagree with last post .  pil

<a href="http://skuper.ru">ламинат и паркет</a> 9m

# August 24, 2008 3:37 PM

pclady said:

WOW! You rock!!! Thanks. Was struggling for 30 minutes!

# May 18, 2009 11:43 PM

Wernerh said:

Faced the exact same issue, but found the solution myself after about 30 minutes.

Very, very, very, VERY surprised this has not been resolved yet. I did this before, using ASP.Net with VB syntax and there it worked just fine!

Anyway, thx for the post, this at least reassures me I'm not being blind to some stupid mistake I might have made ;-)

# September 22, 2009 11:39 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker