A little question about handling nulls from database

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi everyone,

I'm wondering if there is some easy way to deal with null values being
returned in a DataRow.

When I get data from a database (Access) most of it is string based.
However, I don't always know if a particular row will contain any
information in any given column.
Sometimes there will be no information there, in which case I get a null
back.

The way I am getting values out of the DataTable is as follows:

foreach(DataRow row in config.Tables["usenet_accounts"].Rows){
try{
usenetAccount = new UsenetAccount((int)row["account_id"],
(string)row["username"],(string)row["password"],
(string)row["account_name"],
(string)row["email_address"], (string)row["contact_name"],
(string)row["nntp_server"],
(string)row["signature"],
(bool)row["default_account"], (bool)row["auto_monitor"],
(int)row["monitor_interval"]);

usenetAccounts.Add(usenetAccount);
}
}


As you can see there are quite a few columns of data there. Several of them
could return no information.

Is the only way round this to create if statements for each data column and
test to see if the value is null? Or is there an easier way that I'm just
not seeing?!

Thanks everyone

Take care

Simon
 
THe only way I know of is either the test it for null using ISDBNull
beforehand, or writing my SQLStatement like
SELECT ISNULL(myField, '') AS 'myField' etc
 
If you know that a column can contain NULL, then you
could try concatenating an empty string.

Also, DataRow has an IsNull function, but I know you
didn't want to condition each column.
 
JustAsuggestion's solution will work, but suppose an empty string ("")
is a valid entry in a column and null is not. What will you do then?

The best thing for you to do that will fit any circumstance is
habitually check all data that might possibly be null for being null.
In C# the test would be:

if((string)row["username"] == DBNull.Value)
{
Do the right thing;
}
else
{
Do something else;
}

Yes, this takes longer to code, but it is more "bullet proof" and
heck, you can make a macro that writes all the code except the column
name, since this is a very common test made in database work you will
have to deal with time after time.



If you know that a column can contain NULL, then you
could try concatenating an empty string.

Also, DataRow has an IsNull function, but I know you
didn't want to condition each column.
-----Original Message-----
Hi everyone,

I'm wondering if there is some easy way to deal with null values being
returned in a DataRow.

When I get data from a database (Access) most of it is string based.
However, I don't always know if a particular row will contain any
information in any given column.
Sometimes there will be no information there, in which case I get a null
back.

The way I am getting values out of the DataTable is as follows:

foreach(DataRow row in config.Tables ["usenet_accounts"].Rows){
try{
usenetAccount = new UsenetAccount((int)row ["account_id"],
(string)row["username"],(string)row["password"],
(string)row["account_name"],
(string)row["email_address"], (string)row ["contact_name"],
(string)row["nntp_server"],
(string)row["signature"],
(bool)row["default_account"], (bool)row["auto_monitor"],
(int)row["monitor_interval"]);

usenetAccounts.Add(usenetAccount);
}
}


As you can see there are quite a few columns of data there. Several of them
could return no information.

Is the only way round this to create if statements for each data column and
test to see if the value is null? Or is there an easier way that I'm just
not seeing?!

Thanks everyone

Take care

Simon


.

Otis Mukinfus
http://www.otismukinfus.com
 
Back
Top