[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.odbcnet

Handling null values in .NET

Derek Lakin

11/8/2002 10:13:00 AM

I have an MSAccess database table that contains string and numeric columns.
For some rows, sometimes, some of the columns contain null values.

Having used Server Explorer in VS.NET, I now have a strongly-typed dataset
and the necessary OleDbDataAdapter. After the call to Fill when inspecting
the data set in the debugger, all of the null fields (string and int) have
the message: "<error: an exception of type:
{System.Data.StrongTypingException} occurred>".

For the int columns I could understand this being because you can't cast a
null to an int, but I'm not sure about the string columns.

This then causes a failure to occur when trying to insert or update data and
commiting to the database (it can't translate an exception to a valid value
to produce the sql statement) and no doubt causes other databinding issues
as well.

Surely I'm not the first or only person to have null values in columns when
producing a .NET application (using C# in this case)!? Someone must have
come across this and worked out a way round it, the question is how?


4 Answers

Bonnie Berent

11/9/2002 7:19:00 AM

0

Derek,

I've encountered similar problems with NULLs. I don't know if there are
other workarounds, but I have found that if I need to access a particular
field, I have to use the old un-typed notation to first see if it's a NULL
or not. So, if I need to check an integer field, for example like this:

if (ds.MyTable[nRow].MyField == 1)

it will blow up if MyField happens to be NULL. But, if you do this instead:

if (ds.MyTable[nRow]["MyField"] != DBNull.Value && ds.MyTable[nRow].MyField
==1)

then it will handle the NULL situation. I know, it kinda sucks because it
you have to resort to using some of the non-typed dataset notation, but it's
the only way that I've found around it.



~~Bonnie


"Derek Lakin" <dlakin@mass.co.uk> wrote in message
news:uarjedwhCHA.1860@tkmsftngp09...
> I have an MSAccess database table that contains string and numeric
columns.
> For some rows, sometimes, some of the columns contain null values.
>
> Having used Server Explorer in VS.NET, I now have a strongly-typed dataset
> and the necessary OleDbDataAdapter. After the call to Fill when inspecting
> the data set in the debugger, all of the null fields (string and int) have
> the message: "<error: an exception of type:
> {System.Data.StrongTypingException} occurred>".
>
> For the int columns I could understand this being because you can't cast a
> null to an int, but I'm not sure about the string columns.
>
> This then causes a failure to occur when trying to insert or update data
and
> commiting to the database (it can't translate an exception to a valid
value
> to produce the sql statement) and no doubt causes other databinding issues
> as well.
>
> Surely I'm not the first or only person to have null values in columns
when
> producing a .NET application (using C# in this case)!? Someone must have
> come across this and worked out a way round it, the question is how?
>
>


Christoph Wienands

11/10/2002 4:38:00 PM

0

Hi Derek,

"Derek Lakin" <dlakin@mass.co.uk> wrote in message
news:uarjedwhCHA.1860@tkmsftngp09...
> I have an MSAccess database table that contains string and numeric
columns.
> For some rows, sometimes, some of the columns contain null values.

Currently I'm working on the same problem. Here's an article that may help
you:
E:\FH\Diplomarbeit2\Articles and Tutorials\Using Annotations with a Typed
DataSet.htm

CU Christoph


Gary Ranson

11/11/2002 10:12:00 AM

0

Bonnie,

If Derek is using a typed dataget generated with MSDataSetGenerator the
better method would be ...

<typeddataset>.<typeddatatable>.<field>IsNull.

This is a public property and has a getter and setter.

So ...

if(value==null) row.<field>IsNull=true; else row.<field>=value;


"Bonnie Berent" <bonnieb@profitware-online.com> wrote in message
news:#wFfVf7hCHA.2240@tkmsftngp12...
> Derek,
>
> I've encountered similar problems with NULLs. I don't know if there are
> other workarounds, but I have found that if I need to access a particular
> field, I have to use the old un-typed notation to first see if it's a
NULL
> or not. So, if I need to check an integer field, for example like this:
>
> if (ds.MyTable[nRow].MyField == 1)
>
> it will blow up if MyField happens to be NULL. But, if you do this
instead:
>
> if (ds.MyTable[nRow]["MyField"] != DBNull.Value &&
ds.MyTable[nRow].MyField
> ==1)
>
> then it will handle the NULL situation. I know, it kinda sucks because it
> you have to resort to using some of the non-typed dataset notation, but
it's
> the only way that I've found around it.
>
>
>
> ~~Bonnie
>
>
> "Derek Lakin" <dlakin@mass.co.uk> wrote in message
> news:uarjedwhCHA.1860@tkmsftngp09...
> > I have an MSAccess database table that contains string and numeric
> columns.
> > For some rows, sometimes, some of the columns contain null values.
> >
> > Having used Server Explorer in VS.NET, I now have a strongly-typed
dataset
> > and the necessary OleDbDataAdapter. After the call to Fill when
inspecting
> > the data set in the debugger, all of the null fields (string and int)
have
> > the message: "<error: an exception of type:
> > {System.Data.StrongTypingException} occurred>".
> >
> > For the int columns I could understand this being because you can't cast
a
> > null to an int, but I'm not sure about the string columns.
> >
> > This then causes a failure to occur when trying to insert or update data
> and
> > commiting to the database (it can't translate an exception to a valid
> value
> > to produce the sql statement) and no doubt causes other databinding
issues
> > as well.
> >
> > Surely I'm not the first or only person to have null values in columns
> when
> > producing a .NET application (using C# in this case)!? Someone must have
> > come across this and worked out a way round it, the question is how?
> >
> >
>
>


Christoph Wienands

11/11/2002 11:02:00 AM

0

"Christoph Wienands" <cwienands@gmx.de> wrote in message
news:eXM#17MiCHA.4228@tkmsftngp08...

Oops, wrong clipboard :-(

Here's the link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/c...
l/cpconusingannotationswithtypeddataset.asp

CU Christoph