PC Review


Reply
Thread Tools Rate Thread

Data reader and Null values

 
 
Vibhu Srinivasan
Guest
Posts: n/a
 
      14th Jul 2003
I am using data reader for reading tables.
I have some issues with ADO readers, having come from a
java perspective

When reading a table say Account table ( which has two
fields Id(int), Name(varchar)) both can be null

In the C# code i end up doing

int id = (int)reader["Id"];
string name = (string)reader["Name"];

1) If the id or name Name is NULL this blows up with a ull
pointer . So i have to

int columnNum =
reader.GetOrdinalcolumnName);

if(reader.IsDBNull(columnNum)){
then read the column.
}

This int is a value type , how can i represent a NULL
object in the system. Is there a equivalent int object(
something like the Integer class in java to represent a
NULL value.

2) Is there a way to set up reader to retutn a DBNull
object if there are null values in the database.
What i ideally want to do is
reader.GetInteger("ColumnName"){

}
that returns a Integer and similarly reader.SetInteger
(Integer){

}
like i can easily do in the Java world.
Not quite sure why this was left out in .NET

Appreciate any feedback
Vibhu Srinivasan
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      14th Jul 2003
null in a database means unknown value, while a null in c# or java means
unassigned value. there are subtle differences, this is why dbnull is not
mapped to null.

..net value types (unlike java) have no base class. to use them as an object
(called boxing) you just assign them to the base class object, which can
hold a reference to any value type.

int i = 0;
object o = i;

you cast to unbox

i = (int) o;

you can use object to get around the if on the datareader, but you will need
the if on every reference to the object, as a Db.Null or null will not cast
to an int. if you just want to cast Db.Null to 0 or "", then supplay a
static function.

static int ToInt(IDataReader dr, string colName)
{
int columnNum = dr.GetOrdinal(colName);
if (dr.IsDBNull(columnNum))
return 0;
else
return dr.GetInt32(columnNum);
}




"Vibhu Srinivasan" <(E-Mail Removed)> wrote in message
news:002101c34a23$ca0609d0$(E-Mail Removed)...
> I am using data reader for reading tables.
> I have some issues with ADO readers, having come from a
> java perspective
>
> When reading a table say Account table ( which has two
> fields Id(int), Name(varchar)) both can be null
>
> In the C# code i end up doing
>
> int id = (int)reader["Id"];
> string name = (string)reader["Name"];
>
> 1) If the id or name Name is NULL this blows up with a ull
> pointer . So i have to
>
> int columnNum =
> reader.GetOrdinalcolumnName);
>
> if(reader.IsDBNull(columnNum)){
> then read the column.
> }
>
> This int is a value type , how can i represent a NULL
> object in the system. Is there a equivalent int object(
> something like the Integer class in java to represent a
> NULL value.
>
> 2) Is there a way to set up reader to retutn a DBNull
> object if there are null values in the database.
> What i ideally want to do is
> reader.GetInteger("ColumnName"){
>
> }
> that returns a Integer and similarly reader.SetInteger
> (Integer){
>
> }
> like i can easily do in the Java world.
> Not quite sure why this was left out in .NET
>
> Appreciate any feedback
> Vibhu Srinivasan



 
Reply With Quote
 
Vibhu
Guest
Posts: n/a
 
      14th Jul 2003
I do agree that database null and DbNull are different.
But in a business world a NULL for an int field or an O in
the int field may mean different things.
I am aware of the boxing feature. It is not useful though
to just turn an int to an object. Then I have to know what
type it is to cast it back
So i am think of creating owr own types Integer, Float,
Double and then provide static methods
public static Integer GetInteger(IDataReader reader,
string columnName) {
int value;
int columnNum = reader.GetOrdinal
(columnName);
if(reader.IsDBNull(columnNum)){
return null;
}else{
value =(int)reader[columnName];
return new Integer(value);
}

}

public static string GetString(IDataReader reader,
string columnName){
int columnNum = reader.GetOrdinal
(columnName);
if(reader.IsDBNull(columnNum)){
return null;
}else{
return (string)reader[columnName];
}
}
>-----Original Message-----
>null in a database means unknown value, while a null in

c# or java means
>unassigned value. there are subtle differences, this is

why dbnull is not
>mapped to null.
>
>..net value types (unlike java) have no base class. to

use them as an object
>(called boxing) you just assign them to the base class

object, which can
>hold a reference to any value type.
>
> int i = 0;
> object o = i;
>
>you cast to unbox
>
> i = (int) o;
>
>you can use object to get around the if on the

datareader, but you will need
>the if on every reference to the object, as a Db.Null or

null will not cast
>to an int. if you just want to cast Db.Null to 0 or "",

then supplay a
>static function.
>
>static int ToInt(IDataReader dr, string colName)
>{
> int columnNum = dr.GetOrdinal(colName);
> if (dr.IsDBNull(columnNum))
> return 0;
> else
> return dr.GetInt32(columnNum);
>}
>
>
>
>
>"Vibhu Srinivasan" <(E-Mail Removed)> wrote in

message
>news:002101c34a23$ca0609d0$(E-Mail Removed)...
>> I am using data reader for reading tables.
>> I have some issues with ADO readers, having come from a
>> java perspective
>>
>> When reading a table say Account table ( which has two
>> fields Id(int), Name(varchar)) both can be null
>>
>> In the C# code i end up doing
>>
>> int id = (int)reader["Id"];
>> string name = (string)reader["Name"];
>>
>> 1) If the id or name Name is NULL this blows up with a

ull
>> pointer . So i have to
>>
>> int columnNum =
>> reader.GetOrdinalcolumnName);
>>
>> if(reader.IsDBNull(columnNum)){
>> then read the column.
>> }
>>
>> This int is a value type , how can i represent a NULL
>> object in the system. Is there a equivalent int object(
>> something like the Integer class in java to represent a
>> NULL value.
>>
>> 2) Is there a way to set up reader to retutn a DBNull
>> object if there are null values in the database.
>> What i ideally want to do is
>> reader.GetInteger("ColumnName"){
>>
>> }
>> that returns a Integer and similarly reader.SetInteger
>> (Integer){
>>
>> }
>> like i can easily do in the Java world.
>> Not quite sure why this was left out in .NET
>>
>> Appreciate any feedback
>> Vibhu Srinivasan

>
>
>.
>

 
Reply With Quote
 
Gonzalo Medina
Guest
Posts: n/a
 
      14th Jul 2003
Hi Vibhu,

I think it would be a good idea if you tell us
what the business rule is. You said "But in a business
world a NULL for an int field or an O in the int field may
mean different things." Give us an example and you will
get more help.

Maybe what you should do is:
If Id is null return a value like -32768.
If Name is nulll return a values(string) as (UNKNOW).

Using this technique you now that the Id or Name fields
are null.

Thanks!!



>-----Original Message-----
>I do agree that database null and DbNull are different.
>But in a business world a NULL for an int field or an O

in
>the int field may mean different things.
>I am aware of the boxing feature. It is not useful though
>to just turn an int to an object. Then I have to know

what
>type it is to cast it back
>So i am think of creating owr own types Integer, Float,
>Double and then provide static methods
> public static Integer GetInteger(IDataReader reader,
>string columnName) {
> int value;
> int columnNum = reader.GetOrdinal
>(columnName);
> if(reader.IsDBNull(columnNum)){
> return null;
> }else{
> value =(int)reader[columnName];
> return new Integer(value);
> }
>
> }
>
> public static string GetString(IDataReader

reader,
>string columnName){
> int columnNum = reader.GetOrdinal
>(columnName);
> if(reader.IsDBNull(columnNum)){
> return null;
> }else{
> return (string)reader[columnName];
> }
> }
>>-----Original Message-----
>>null in a database means unknown value, while a null in

>c# or java means
>>unassigned value. there are subtle differences, this is

>why dbnull is not
>>mapped to null.
>>
>>..net value types (unlike java) have no base class. to

>use them as an object
>>(called boxing) you just assign them to the base class

>object, which can
>>hold a reference to any value type.
>>
>> int i = 0;
>> object o = i;
>>
>>you cast to unbox
>>
>> i = (int) o;
>>
>>you can use object to get around the if on the

>datareader, but you will need
>>the if on every reference to the object, as a Db.Null or

>null will not cast
>>to an int. if you just want to cast Db.Null to 0 or "",

>then supplay a
>>static function.
>>
>>static int ToInt(IDataReader dr, string colName)
>>{
>> int columnNum = dr.GetOrdinal(colName);
>> if (dr.IsDBNull(columnNum))
>> return 0;
>> else
>> return dr.GetInt32(columnNum);
>>}
>>
>>
>>
>>
>>"Vibhu Srinivasan" <(E-Mail Removed)> wrote in

>message
>>news:002101c34a23$ca0609d0$(E-Mail Removed)...
>>> I am using data reader for reading tables.
>>> I have some issues with ADO readers, having come from a
>>> java perspective
>>>
>>> When reading a table say Account table ( which has two
>>> fields Id(int), Name(varchar)) both can be null
>>>
>>> In the C# code i end up doing
>>>
>>> int id = (int)reader["Id"];
>>> string name = (string)reader["Name"];
>>>
>>> 1) If the id or name Name is NULL this blows up with a

>ull
>>> pointer . So i have to
>>>
>>> int columnNum =
>>> reader.GetOrdinalcolumnName);
>>>
>>> if(reader.IsDBNull(columnNum)){
>>> then read the column.
>>> }
>>>
>>> This int is a value type , how can i represent a NULL
>>> object in the system. Is there a equivalent int object(
>>> something like the Integer class in java to represent a
>>> NULL value.
>>>
>>> 2) Is there a way to set up reader to retutn a DBNull
>>> object if there are null values in the database.
>>> What i ideally want to do is
>>> reader.GetInteger("ColumnName"){
>>>
>>> }
>>> that returns a Integer and similarly reader.SetInteger
>>> (Integer){
>>>
>>> }
>>> like i can easily do in the Java world.
>>> Not quite sure why this was left out in .NET
>>>
>>> Appreciate any feedback
>>> Vibhu Srinivasan

>>
>>
>>.
>>

>.
>

 
Reply With Quote
 
Kathleen Dollard
Guest
Posts: n/a
 
      16th Jul 2003
Vibhu,

These types have already been created. They are the System.Data.SQLTypes.
THe SQLDataReader provides exactly teh methods you are talking about
writing.

Kathleen

"Vibhu" <(E-Mail Removed)> wrote in message
news:007b01c34a34$0dc895b0$(E-Mail Removed)...
> I do agree that database null and DbNull are different.
> But in a business world a NULL for an int field or an O in
> the int field may mean different things.
> I am aware of the boxing feature. It is not useful though
> to just turn an int to an object. Then I have to know what
> type it is to cast it back
> So i am think of creating owr own types Integer, Float,
> Double and then provide static methods
> public static Integer GetInteger(IDataReader reader,
> string columnName) {
> int value;
> int columnNum = reader.GetOrdinal
> (columnName);
> if(reader.IsDBNull(columnNum)){
> return null;
> }else{
> value =(int)reader[columnName];
> return new Integer(value);
> }
>
> }
>
> public static string GetString(IDataReader reader,
> string columnName){
> int columnNum = reader.GetOrdinal
> (columnName);
> if(reader.IsDBNull(columnNum)){
> return null;
> }else{
> return (string)reader[columnName];
> }
> }
> >-----Original Message-----
> >null in a database means unknown value, while a null in

> c# or java means
> >unassigned value. there are subtle differences, this is

> why dbnull is not
> >mapped to null.
> >
> >..net value types (unlike java) have no base class. to

> use them as an object
> >(called boxing) you just assign them to the base class

> object, which can
> >hold a reference to any value type.
> >
> > int i = 0;
> > object o = i;
> >
> >you cast to unbox
> >
> > i = (int) o;
> >
> >you can use object to get around the if on the

> datareader, but you will need
> >the if on every reference to the object, as a Db.Null or

> null will not cast
> >to an int. if you just want to cast Db.Null to 0 or "",

> then supplay a
> >static function.
> >
> >static int ToInt(IDataReader dr, string colName)
> >{
> > int columnNum = dr.GetOrdinal(colName);
> > if (dr.IsDBNull(columnNum))
> > return 0;
> > else
> > return dr.GetInt32(columnNum);
> >}
> >
> >
> >
> >
> >"Vibhu Srinivasan" <(E-Mail Removed)> wrote in

> message
> >news:002101c34a23$ca0609d0$(E-Mail Removed)...
> >> I am using data reader for reading tables.
> >> I have some issues with ADO readers, having come from a
> >> java perspective
> >>
> >> When reading a table say Account table ( which has two
> >> fields Id(int), Name(varchar)) both can be null
> >>
> >> In the C# code i end up doing
> >>
> >> int id = (int)reader["Id"];
> >> string name = (string)reader["Name"];
> >>
> >> 1) If the id or name Name is NULL this blows up with a

> ull
> >> pointer . So i have to
> >>
> >> int columnNum =
> >> reader.GetOrdinalcolumnName);
> >>
> >> if(reader.IsDBNull(columnNum)){
> >> then read the column.
> >> }
> >>
> >> This int is a value type , how can i represent a NULL
> >> object in the system. Is there a equivalent int object(
> >> something like the Integer class in java to represent a
> >> NULL value.
> >>
> >> 2) Is there a way to set up reader to retutn a DBNull
> >> object if there are null values in the database.
> >> What i ideally want to do is
> >> reader.GetInteger("ColumnName"){
> >>
> >> }
> >> that returns a Integer and similarly reader.SetInteger
> >> (Integer){
> >>
> >> }
> >> like i can easily do in the Java world.
> >> Not quite sure why this was left out in .NET
> >>
> >> Appreciate any feedback
> >> Vibhu Srinivasan

> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null values in data base =?Utf-8?B?Q29hY2hCYXJrZXJPSlBX?= Microsoft Access 3 14th Jul 2006 02:59 PM
Retrieving values from Data Reader by column name Dylan Parry Microsoft C# .NET 5 4th Jul 2006 02:27 PM
Data binding and Null Values =?Utf-8?B?QmVuIFJlZXNl?= Microsoft Dot NET 1 25th Jul 2005 04:18 PM
Passing null to SQL using Data Reader. Willie Neal Microsoft ADO .NET 1 3rd Apr 2004 10:28 AM
checking for a null value in a data reader ]-[aTc]-[ Microsoft ASP .NET 1 2nd Dec 2003 05:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:18 AM.