[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.odbcnet

OleDbDataReader and null values

Peter K

4/11/2005 8:44: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


3 Answers

Paul Clement

4/11/2005 6:06:00 PM

0

On Mon, 11 Apr 2005 10:44:05 +0200, "Peter Kirk" <pk@alpha-solutions.dk> wrote:

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

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


Paul
~~~~
Microsoft MVP (Visual Basic)

Peter K

4/12/2005 6:53:00 AM

0

"Paul Clement" <UseAdddressAtEndofMessage@swspectrum.com> skrev i en
meddelelse news:23fl515evsa7l8gn8hlb8u7dnoj94uietc@4ax.com...
> On Mon, 11 Apr 2005 10:44:05 +0200, "Peter Kirk" <pk@alpha-solutions.dk>
> wrote:
>
> &#164; Hi
> &#164;
> &#164; I am using OleDbDataReader to get some data from a database. I have
> noticed
> &#164; I get an exception if one of the database columns contains a null value.
> &#164; Sure enough, the documentation says I have to call IsDbNull first, to
> avoid
> &#164; an error.
> &#164;
> &#164; Can this be right? Is it possible just to call GetString, for example,
> and
> &#164; get a null if the database value is null? What is the rationale behind
> this?
> &#164; It seems to mean that all my "Gets" require I write a test for null
> &#164; 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("ITTSDBConnectionString");
OleDbConnection myConnection = new
OleDbConnection(myConnectionString);
myConnection.Open();
return myConnection;
}


Thanks,
Peter


Paul Clement

4/12/2005 1:49:00 PM

0

On Tue, 12 Apr 2005 08:52:45 +0200, "Peter Kirk" <pk@alpha-solutions.dk> wrote:

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

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)