468,780 Members | 2,200 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

NULL values in dataset

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
May 3 '07 #1
9 3006
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

May 3 '07 #2
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


May 3 '07 #3
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



May 3 '07 #4
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



May 3 '07 #5
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
>




May 3 '07 #6
if there is a value I'm fine its when the field is blank thats killing me.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
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.com>
wrote 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
>>
>
>




May 3 '07 #7
Hi,

"GotDotNet?" <cs*********@optonline.netwrote in message
news:Oc**************@TK2MSFTNGP02.phx.gbl...
if there is a value I'm fine its when the field is blank thats killing me.
Then just check for null values before operating in the field
May 3 '07 #8
Null or Blank can be a real nuisance. Net 2.0 / VS2005 implements the handy

if (String.IsNullOrEmpty(sSomeStringValue)) that allows you to test
sSomeStringValue for Null or Blank. In Net 1.0 and Net 1.1 we basically
'rolled our own' version by creating a generic CheckForBlankOrNull(object)
class that returned null if 'object' was blank or null.

Best of luck

Barry
in Oregon

"GotDotNet?" <cs*********@optonline.netwrote in message
news:Oc**************@TK2MSFTNGP02.phx.gbl...
if there is a value I'm fine its when the field is blank thats killing me.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP03.phx.gbl...
> 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.com>
wrote 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
>>>
>>
>>
>
>




May 3 '07 #9
this is a nightmare right now

"frostbb" <ba***********@remove-this-spam-filter.state.or.uswrote in
message news:eH**************@TK2MSFTNGP02.phx.gbl...
Null or Blank can be a real nuisance. Net 2.0 / VS2005 implements the
handy

if (String.IsNullOrEmpty(sSomeStringValue)) that allows you to test
sSomeStringValue for Null or Blank. In Net 1.0 and Net 1.1 we basically
'rolled our own' version by creating a generic CheckForBlankOrNull(object)
class that returned null if 'object' was blank or null.

Best of luck

Barry
in Oregon

"GotDotNet?" <cs*********@optonline.netwrote in message
news:Oc**************@TK2MSFTNGP02.phx.gbl...
>if there is a value I'm fine its when the field is blank thats killing
me.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>> 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.com>
wrote 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.com>
>wrote 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
>>>>
>>>
>>>
>>
>>
>
>




May 3 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by sameer afsar | last post: by
11 posts views Thread by eddy de boer | last post: by
4 posts views Thread by chambersdon | last post: by
14 posts views Thread by rodchar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.