• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1657
  • Last Modified:

Inserting data with .net IDbCommand and IDbDataParameter

So, i have the following code:

IDbCommand masterMeterCommand = dbConn.CreateCommand();
masterMeterCommand.CommandText = "insert into metermaster (MeterId, UtilityId, ManufacturerId) values (@MeterId,2,1)";

IDbDataParameter dbIdParam = masterMeterCommand.CreateParameter();
dbIdParam.ParameterName = "@MeterId";
dbIdParam.Value = 1;

masterMeterCommand.Parameters.Add(dbIdParam);
masterMeterCommand.ExecuteNonQuery();

It always fails at ExecuteNonQuery with a "Column 'MeterId' cannot be null". So it seems that the parameter is not being populated or something. If i execute:

insert into metermaster (MeterId, UtilityId, ManufacturerId) values (1,2,1)

then it works just fine...what gives?

0
MarkoBarko
Asked:
MarkoBarko
1 Solution
 
MarkoBarkoAuthor Commented:
Never mind, answered my own question...

the syntax for the prepared statement differs depending on the underlying provider.

The syntax above works on MS SQL which uses the @ symbol to mark variables...i was using the MySQL provider which uses question marks for the same purpose...

So much for writing portable queries...
0
 
EE_AutoDeleterCommented:
MarkoBarko,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now