HighTechTalks DotNet Forums  

OleDbDataReader and null values

Dotnet Framework (ODBC.net) microsoft.public.dotnet.framework.odbcnet


Discuss OleDbDataReader and null values in the Dotnet Framework (ODBC.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Peter Kirk
 
Posts: n/a

Default OleDbDataReader and null values - 04-11-2005 , 04:44 AM






Hi

I am using OleDbDataReader to get some data from a database. I have noticed
I get an exception if one of the database columns contains a null value.
Sure enough, the documentation says I have to call IsDbNull first, to avoid
an error.

Can this be right? Is it possible just to call GetString, for example, and
get a null if the database value is null? What is the rationale behind this?
It seems to mean that all my "Gets" require I write a test for null
values...

Thanks,
Peter



Reply With Quote
  #2  
Old   
Paul Clement
 
Posts: n/a

Default Re: OleDbDataReader and null values - 04-11-2005 , 02:06 PM






On Mon, 11 Apr 2005 10:44:05 +0200, "Peter Kirk" <pk (AT) alpha-solutions (DOT) dk> wrote:

¤ Hi
¤
¤ I am using OleDbDataReader to get some data from a database. I have noticed
¤ I get an exception if one of the database columns contains a null value.
¤ Sure enough, the documentation says I have to call IsDbNull first, to avoid
¤ an error.
¤
¤ Can this be right? Is it possible just to call GetString, for example, and
¤ get a null if the database value is null? What is the rationale behind this?
¤ It seems to mean that all my "Gets" require I write a test for null
¤ values...

At what point to you get the error? Code example?


Paul
~~~~
Microsoft MVP (Visual Basic)

Reply With Quote
  #3  
Old   
Peter Kirk
 
Posts: n/a

Default Re: OleDbDataReader and null values - 04-12-2005 , 02:52 AM



"Paul Clement" <UseAdddressAtEndofMessage (AT) swspectrum (DOT) com> skrev i en
meddelelse news:23fl515evsa7l8gn8hlb8u7dnoj94uietc (AT) 4ax (DOT) com...
Quote:
On Mon, 11 Apr 2005 10:44:05 +0200, "Peter Kirk" <pk (AT) alpha-solutions (DOT) dk
wrote:

¤ Hi
¤
¤ I am using OleDbDataReader to get some data from a database. I have
noticed
¤ I get an exception if one of the database columns contains a null value.
¤ Sure enough, the documentation says I have to call IsDbNull first, to
avoid
¤ an error.
¤
¤ Can this be right? Is it possible just to call GetString, for example,
and
¤ get a null if the database value is null? What is the rationale behind
this?
¤ It seems to mean that all my "Gets" require I write a test for null
¤ values...

At what point to you get the error? Code example?
Hi, below is some c# code which retrieves a list of "Tasktype" objects.
Tasktype has two attributes: name & description.

There is a row in the database which has description = null.
This code works, because I have added the check for a null value when I
extract the data from the resultset:

t.Description = rs.IsDBNull(1) ? null : rs.GetString(1);

Extraction from the resultset will fail on the null data if I just have:

t.Description = rs.GetString(1);


// Retrieves a list of Tasktype objects.
public IList GetAllTasktypes()
{
OleDbCommand myCommand = null;
try
{
string mySelectQuery = "SELECT Name, Description FROM t_Tasktype
ORDER BY Name";
myCommand = new OleDbCommand(mySelectQuery);
myCommand.Connection = GetConnection();
OleDbDataReader resultSet = myCommand.ExecuteReader();
IList tasktypes = null;
if ( (resultSet!=null) && (resultSet.HasRows))
{
tasktypes = new ArrayList();
while (resultSet.Read())
{
Tasktype t = new Tasktype();
t.Name = rs.IsDBNull(0) ? null : rs.GetString(0);
t.Description = rs.IsDBNull(1) ? null : rs.GetString(1);

tasktypes.Add(t);
}
}
return tasktypes;
}
finally
{
if ( (myCommand!=null) && (myCommand.Connection!=null))
CloseConnection(myCommand.Connection);
}
}

private OleDbConnection GetConnection()
{
string myConnectionString =
ConfigurationSettings.AppSettings.Get("ITTSDBConne ctionString");
OleDbConnection myConnection = new
OleDbConnection(myConnectionString);
myConnection.Open();
return myConnection;
}


Thanks,
Peter




Reply With Quote
  #4  
Old   
Paul Clement
 
Posts: n/a

Default Re: OleDbDataReader and null values - 04-12-2005 , 09:48 AM



On Tue, 12 Apr 2005 08:52:45 +0200, "Peter Kirk" <pk (AT) alpha-solutions (DOT) dk> wrote:

¤ > ¤ Hi
¤ > ¤
¤ > ¤ I am using OleDbDataReader to get some data from a database. I have
¤ > noticed
¤ > ¤ I get an exception if one of the database columns contains a null value.
¤ > ¤ Sure enough, the documentation says I have to call IsDbNull first, to
¤ > avoid
¤ > ¤ an error.
¤ > ¤
¤ > ¤ Can this be right? Is it possible just to call GetString, for example,
¤ > and
¤ > ¤ get a null if the database value is null? What is the rationale behind
¤ > this?
¤ > ¤ It seems to mean that all my "Gets" require I write a test for null
¤ > ¤ values...
¤ >

Yeah, I don't believe there is any way around this if you need to call a method when retrieving the
value. Null propagation (concatenating an empty string) used to be shortcut method for handling this
type of condition but it cannot be done through a method call (such as GetString).


Paul
~~~~
Microsoft MVP (Visual Basic)

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.