Well, you can't change a DBNull.Value to an int. The DataSet will take
it, because it performs conversions internally when the value is set.
Your best bet would be to do use a Nullable<int(or use the C#
shorthand, int?) like so:
// Declare the nullable int.
int? id = null;
// If there is an id, then convert it.
if (dr["id"] != DBNull.Value)
{
// Convert the number.
id = (int) dr["id"];
}
Something tells me that the underlying column is an integer, if so, you
can just unbox the value returned to you, instead of calling Convert.
Also, you don't need to call ToString, because if the value is null, the
value returned to you will be DBNull.Value.
Now, if you go this route, all of your code will have to check against
null, and anyplace you pass this value, you will have to make sure the
parameter is of type int?. The reason for this is that an implicit
conversion will take place if you pass it in place of an int (not a nullable
int) and it will pass 0, which I don't think you want.
The better solution here would be to just access the value from the row
when you need it and always check for DBNull.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"GotDotNet?" <cs*********@optonline.netwrote in message
news:Oe**************@TK2MSFTNGP06.phx.gbl...
>i tried this
if(dr["id"].toString != null)
{
id = Convert.ToInt32(dr["id"];
}
else
{
id = System.DBNull;
}
now ID needs to be an int as its an int in the db table I need to insert
it into.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:u6**************@TK2MSFTNGP05.phx.gbl...
> Can you show the code you are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"GotDotNet?" <cs*********@optonline.netwrote in message
news:u7**************@TK2MSFTNGP03.phx.gbl...
>>whats the syntax in C# do that? its the C# piece giving me the issue.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:O8**************@TK2MSFTNGP02.phx.gbl...
If the field is a database null, then pass DBNull.Value for the
value. You should also be able to compare against this value.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"GotDotNet?" <cs*********@optonline.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl. ..
>I have a dataset and I have to loop through it and some of the values
>for an insertition into the db. Some of the fields are integers and
>booleans but contain a NULL in the field.
>
how can I check for a NULL and if its NULL insert DBNull into the db?
Currently I'm getting error message
System.DBNull.System.IConvertible.ToBoolean( )
>
>
now i need to get this field and insert a NULL into the db is the
values from the dataset are NULL
>