Groups | Blog | Home
all groups > dotnet odbc.net > april 2005 >

dotnet odbc.net : OleDbDataReader and null values



Peter Kirk
4/11/2005 12:00:00 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

Paul Clement
4/11/2005 1:06:05 PM
[quoted text, click to view]

¤ 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
~~~~
Peter Kirk
4/12/2005 12:00:00 AM
"Paul Clement" <UseAdddressAtEndofMessage@swspectrum.com> skrev i en
meddelelse news:23fl515evsa7l8gn8hlb8u7dnoj94uietc@4ax.com...
[quoted text, click to view]

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("ITTSDBConnectionString");
OleDbConnection myConnection = new
OleDbConnection(myConnectionString);
myConnection.Open();
return myConnection;
}


Thanks,
Peter

Paul Clement
4/12/2005 8:48:32 AM
[quoted text, click to view]

¤ > ¤ 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
~~~~
AddThis Social Bookmark Button