473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3257
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Juan | last post by:
When populating a dataset I get an error if the data being retrieved includes a string field with a Null value: "Not valid store type: DBNull". What kinf of data type can i define in the dataset to...
0
by: richard | last post by:
Hi, I have a Schema for a Dataset which I am populating from Excel. One of the columns is a string datatype. When I Fill the Dataset I want any Null values to be set to a string value. I have...
2
by: Jeremy Chapman | last post by:
Populating a typed dataset from xml document: I created an xml schema (attached below), generated a typed dataset from it, and then programatically I tried to populate the typed dataset by...
0
by: sameer afsar | last post by:
Hello, I have a column which majority of the time the data in it is numeric. There are instances where it can be alpha-numeric and that is why i kept that column as a string. Without getting...
11
by: eddy de boer | last post by:
Hello, in my aspx page I have the followong code: <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> .... <%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))...
4
by: chambersdon | last post by:
I have an application that needs to insert nulls into the database and I don't seem to be able to do this. I am currently trying to do this with a Typed DataSet but I can't seem to Insert Nulls...
14
by: rodchar | last post by:
hey all, i have a winapp that imports a text file into a typed dataset using the data adapter. some of the values are null. is there a way to remove the null values and put an empty string for...
4
by: Bobby Edward | last post by:
I have an xsd dataset. I created a simple query called GetDataByUserId. I can preview the data fine! I created a very simple BLL function that calls it and returns a datatable. When I run...
5
by: hollyquinn | last post by:
Hi I am working with a web application where I am selecting values from a SQL Server 2005 database and then loading the values into different controls on my page. Most of the values load with no...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.