NULL values in dataset

G

GotDotNet?

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

GotDotNet?

whats the syntax in C# do that? its the C# piece giving me the issue.
Nicholas Paldino said:
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]
- (e-mail address removed)

GotDotNet? said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

Can you show the code you are using?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GotDotNet? said:
whats the syntax in C# do that? its the C# piece giving me the issue.
Nicholas Paldino said:
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]
- (e-mail address removed)

GotDotNet? said:
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
 
G

GotDotNet?

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 said:
Can you show the code you are using?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GotDotNet? said:
whats the syntax in C# do that? its the C# piece giving me the issue.
Nicholas Paldino said:
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]
- (e-mail address removed)

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

GotDotNet? said:
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 said:
Can you show the code you are using?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GotDotNet? said:
whats the syntax in C# do that? its the C# piece giving me the issue.
in message 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]
- (e-mail address removed)

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
 
G

GotDotNet?

if there is a value I'm fine its when the field is blank thats killing me.


Nicholas Paldino said:
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]
- (e-mail address removed)

GotDotNet? said:
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 said:
Can you show the code you are using?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

whats the syntax in C# do that? its the C# piece giving me the issue.
"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message 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]
- (e-mail address removed)

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

GotDotNet? said:
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
 
G

GotDotNet?

this is a nightmare right now

frostbb said:
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? said:
if there is a value I'm fine its when the field is blank thats killing
me.


Nicholas Paldino said:
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]
- (e-mail address removed)

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]" <[email protected]>
wrote in message Can you show the code you are using?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

whats the syntax in C# do that? its the C# piece giving me the issue.
"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message 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]
- (e-mail address removed)

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
 
F

frostbb

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? said:
if there is a value I'm fine its when the field is blank thats killing me.


Nicholas Paldino said:
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]
- (e-mail address removed)

GotDotNet? said:
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.


in message Can you show the code you are using?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

whats the syntax in C# do that? its the C# piece giving me the issue.
"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message 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]
- (e-mail address removed)

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
 
M

Maulik Patel

if (dSet.Tables[0].Rows.IsNull("Price") == true)
{
Price = "";
}
else
{
Price = String.Format("{0:c}",Convert.ToDouble(dSet.Tables[0].Rows["Price"].ToString()));
}

Use the above code as per your requirement. To check is the column value null or not use as follow:

if (dSet.Tables[0].Rows.IsNull("Price") == true)



GotDotNet? wrote:

NULL values in dataset
03-May-07

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 messag
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

Previous Posts In This Thread:

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 messag
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

If the field is a database null, then pass DBNull.Value for the value.
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
- (e-mail address removed)


whats the syntax in C# do that?
whats the syntax in C# do that? its the C# piece giving me the issue.

Can you show the code you are using?
Can you show the code you are using

-
- Nicholas Paldino [.NET/C# MVP
- (e-mail address removed)

i tried thisif(dr["id"].toString !
i tried thi

if(dr["id"].toString != null

id = Convert.ToInt32(dr["id"]

els

id = System.DBNull


now ID needs to be an int as its an int in the db table I need to insert i
into.

Well, you can't change a DBNull.Value to an int.
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
- (e-mail address removed)


if there is a value I'm fine its when the field is blank thats killing me.
if there is a value I am fine its when the field is blank thats killing me.

Hi,"GotDotNet?
Hi,


Then just check for null values before operating in the field

Re: NULL values in dataset
this is a nightmare right now


Null or Blank can be a real nuisance. Net 2.
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



Submitted via EggHeadCafe - Software Developer Portal of Choice
Server Side Processing in ADO.NET/WCF Data Services
http://www.eggheadcafe.com/tutorial...f-4f6f92a76585/server-side-processing-in.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top