views:

131

answers:

3

Hi,

I wish to pass Null Value to the parameter as follow:

_db.AddInParameter(dbCommand, "Id", DBNull.Value, myContactPerson.Id);

I am receiving the following error :

"can not convert "System.DBNull to System.Data.DbType".

I know the meaning of this error.

But i need to supply null value to myContactPerson.Id

How can i achieve this ?

A: 

DBType should be passed in that parameter and should match your the dbtype (string, int, etc.) for the table that you are comparing with in your database. You would replace your value field "myContactPerson.Id" with DBNull.Value to always pass the null value.

Jay
Here the Type is Int.
_db.AddInParameter(dbCommand, "Id", System.Data.DBType.Int32, 0); Don't think you can pass null to an Int db type....Better option would be to have it autogenerate.
Jay
+1  A: 

If myContactPerson.Id isn't an auto-number, then why not just pass 0.

Lucas McCoy
yes .I achieved the result by passing Zero.sometimes unbale to think the other way round. :)
@rengaseshan: Your concentrating to hard. I do that all the time. I find it's best to just take a break, and when your not thinking about the problem, it'll just come to you. ;-)
Lucas McCoy
A: 

As per ADO.NET, you can pass DBNull to any DbParameter.

The code should be rewritten as:


_db.AddInParameter(dbCommand, "Id", DbType.Int32, DBNull.Value);
Alfred Myers