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?

LVL 1
MarkoBarkoAsked:
Who is Participating?
 
EE_AutoDeleterConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.