UPDATE problems with Parameters

  • Thread starter Thread starter MW
  • Start date Start date
M

MW

Hello,

I have a problem with parameters in OleDbCommand in Access 2000 database.
For example this code works and updates the row:
OleDbCommand comm = new OleDbCommand("UPDATE DataTable SET ContactID =
'Marcin', Test = 'just a test' WHERE RowID = @RowID ;",
this.databaseConnection);

comm.Parameters.Add("@RowID", OleDbType.Integer);

comm.Parameters["@RowID"].Value = 14;



But if I just change immediate substitution from ContactID to a parameter,
nothing works. Does anyone have any idea why it happens this way ?

OleDbCommand comm = new OleDbCommand("UPDATE DataTable SET ContactID =
@ContactID, Test = 'just a test' WHERE RowID = @RowID ;",
this.databaseConnection);

comm.Parameters.Add("@RowID", OleDbType.Integer);

comm.Parameters.Add("@ContactID", OleDbType.VarWChar, 50);

comm.Parameters["@RowID"].Value = 14;

comm.Parameters["@ContactID"].Value = "Marcin Mastah";



Above code, strangly doesn't work. Any ideas why?

Thanks a lot for all the help
 
try to change order in which you add parameters to

comm.Parameters.Add("@ContactID", OleDbType.VarWChar, 50);
comm.Parameters.Add("@RowID", OleDbType.Integer);

HTH

Peter
 
It work! It is very strange BTW, but what can we do :D

Dzieki stary za pomoc, jestes w tym naprawde dobry.
 
It is not stronge, it is the way OleDb namespace works: parameter name is
not used, instead, parameter's value is read in the sequence when it is
added to Parameters collection.

There was discussion on this topic in this group. Since most ADO.NET
books/examples use SQL Server, this issue is rarely mentioned. Oddly enough,
many new programmers starts .NET using Access, because it is easier to get
satred than SQL Server/MSDE, and they (including me several years ago) run
into this issue and cannot figure out what is wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top