473,569 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.S ystem.IConverti ble.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 3268
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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:%2******** ********@TK2MSF TNGP04.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.S ystem.IConverti ble.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.c omwrote in
message news:O8******** ******@TK2MSFTN GP02.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:%2******** ********@TK2MSF TNGP04.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.IConvert ible.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:u7******** ******@TK2MSFTN GP03.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.c omwrote
in message news:O8******** ******@TK2MSFTN GP02.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:%2******* *********@TK2MS FTNGP04.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.IConver tible.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.c omwrote in
message news:u6******** ******@TK2MSFTN GP05.phx.gbl...
Can you show the code you are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:u7******** ******@TK2MSFTN GP03.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.c omwrote
in message news:O8******** ******@TK2MSFTN GP02.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:%2****** **********@TK2M SFTNGP04.phx.gb l...
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.DBNul l.System.IConve rtible.ToBoolea n()
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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:Oe******** ******@TK2MSFTN GP06.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.c omwrote
in message news:u6******** ******@TK2MSFTN GP05.phx.gbl...
> Can you show the code you are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:u7******* *******@TK2MSFT NGP03.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.c omwrote
in message news:O8******** ******@TK2MSFTN GP02.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.co m

"GotDotNet ?" <cs*********@op tonline.netwrot e in message
news:%2***** ***********@TK2 MSFTNGP04.phx.g bl...
>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.DBNu ll.System.IConv ertible.ToBoole an()
>
>
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.c omwrote in
message news:%2******** ********@TK2MSF TNGP03.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:Oe******** ******@TK2MSFTN GP06.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.c omwrote
in message news:u6******** ******@TK2MSFTN GP05.phx.gbl...
>> Can you show the code you are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:u7****** ********@TK2MSF TNGP03.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.c om>
wrote in message news:O8******** ******@TK2MSFTN GP02.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.co m
>
"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:%2**** ************@TK 2MSFTNGP04.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
>>boolean s but contain a NULL in the field.
>>
>how can I check for a NULL and if its NULL insert DBNull into the db?
>Currentl y I'm getting error message
>System.DBN ull.System.ICon vertible.ToBool ean()
>>
>>
>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*********@op tonline.netwrot e in message
news:Oc******** ******@TK2MSFTN GP02.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.IsNullO rEmpty(sSomeStr ingValue)) that allows you to test
sSomeStringValu e for Null or Blank. In Net 1.0 and Net 1.1 we basically
'rolled our own' version by creating a generic CheckForBlankOr Null(object)
class that returned null if 'object' was blank or null.

Best of luck

Barry
in Oregon

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:Oc******** ******@TK2MSFTN GP02.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.c omwrote
in message news:%2******** ********@TK2MSF TNGP03.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:Oe******* *******@TK2MSFT NGP06.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.c omwrote
in message news:u6******** ******@TK2MSFTN GP05.phx.gbl...
Can you show the code you are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"GotDotNet ?" <cs*********@op tonline.netwrot e in message
news:u7***** *********@TK2MS FTNGP03.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.c om>
wrote in message news:O8******** ******@TK2MSFTN GP02.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.co m
>>
>"GotDotNet ?" <cs*********@op tonline.netwrot e in message
>news:%2*** *************@T K2MSFTNGP04.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
>>>boolea ns 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.DB Null.System.ICo nvertible.ToBoo lean()
>>>
>>>
>>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******** ******@TK2MSFTN GP02.phx.gbl...
Null or Blank can be a real nuisance. Net 2.0 / VS2005 implements the
handy

if (String.IsNullO rEmpty(sSomeStr ingValue)) that allows you to test
sSomeStringValu e for Null or Blank. In Net 1.0 and Net 1.1 we basically
'rolled our own' version by creating a generic CheckForBlankOr Null(object)
class that returned null if 'object' was blank or null.

Best of luck

Barry
in Oregon

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:Oc******** ******@TK2MSFTN GP02.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.c omwrote
in message news:%2******** ********@TK2MSF TNGP03.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.co m

"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:Oe****** ********@TK2MSF TNGP06.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.c om>
wrote in message news:u6******** ******@TK2MSFTN GP05.phx.gbl...
Can you show the code you are using?
>
>
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
>
"GotDotNet? " <cs*********@op tonline.netwrot e in message
news:u7**** **********@TK2M SFTNGP03.phx.gb l...
>whats the syntax in C# do that? its the C# piece giving me the issue.
>"Nichola s Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om>
>wrote in message news:O8******** ******@TK2MSFTN GP02.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.co m
>>>
>>"GotDotNe t?" <cs*********@op tonline.netwrot e in message
>>news:%2** **************@ TK2MSFTNGP04.ph x.gbl...
>>>>I have a dataset and I have to loop through it and some of the
>>>>value s for an insertition into the db. Some of the fields are
>>>>integer s 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.D BNull.System.IC onvertible.ToBo olean()
>>>>
>>>>
>>>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
1282
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 allow null values for strings? Any help is very welcome. Juan.
0
1833
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 seen varies articles using this namespace xmlns:codegen="urn:schemas-microsoft-com:xml-msprop" This allows you to use the codegen:nullValue="" in...
2
9501
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 calling its ReadXml method. I keep getting a constraint exception. I have validated that my xml matches the schema using xmlspy. I've included the...
0
1048
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 into too much detail, i have a csv file with the data and an xsd file with the schema for the dataset. When i query the data from the csv file, any...
11
34475
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")) %> .... </ItemTemplate>
4
2266
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 (or udpate columns with a null value). I have a column in a test table called 'anInteger'. The Typed DataSet for this element has minOccurs="0" and...
14
1261
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 character fields and 0 for numeric fields? is there a possible expression i can use in the typed dataset properties for each field to rid the...
4
21961
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 the code I get.... ======================= Server Error in '/MyCompanyMyProject' Application.
5
1749
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 problem, except for those columns where the value of the selected row in the column could be a null. I get the following error message: The value for...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.