Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

Typed DataSet: "Cannot get value because it is DBNull": Why still using it?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DraguVaso - 14 Jul 2005 13:43 GMT
Hi,

Something I don't understand about a Typed DataSet: When a value in the
DataSet is DBNull, it throws this error: "Cannot get value because it is
DBNull".

But aren't Typed DataSets invented to make life easier, to be able to get to
tge Tables and Values with less code, in less time? But with this thing you
need to add a Try-Catch around every statement when using the value, add for
each value a default value in your DataSet (and you have to redo it each
time something changes to your DataSet!), or don't allow Null-values in any
of your Tables in your Sql Server..

So why are people still using these things? Did anybody find an easy, fast
and good working solution to get around this problem?

Pieter
cody - 14 Jul 2005 13:56 GMT
If the value in the dataset is null what do you expect the getter to return?
How do you expect they will cast DBNull in, say, int? If you do not
need/want nulls give the DataColumn a defaultvalue and set allowdbnull to
false.
You can also use the untyped indexer of the datarow which will return what
is in the row regardless which type and wheather it is null or not.

And one important thing: please do not crosspost, this is inpolite.

Hope it helps.

> Hi,
>
[quoted text clipped - 13 lines]
>
> Pieter
DraguVaso - 14 Jul 2005 14:23 GMT
Well, I expect it to return DBNull, and not an error! DBNull is actually
also some kind of value, so throwing an exception isn't really a nice thing
in my opinion, hehe :-) When using the Untyped DataSet it returns DBNull
too, so i don't see a reason why they have changed this with a Typed
DataSet...

And yes indeed you can use the untyped indexer every time: but again: why
use a Typed DataSet if you need to call everytime the untyped to read the
value's? It jsut doesn't make any sense to me :-(

Pieter

PS: I didn't croospost this? I just send it to 4 newgroups that seem
logically concerned to this: framework, adonet (it has something to do with
it), vb (which I uses), general (because it happens in other languages too).
But anyways my apologizes if I harmed anybody with this.

> If the value in the dataset is null what do you expect the getter to return?
> How do you expect they will cast DBNull in, say, int? If you do not
[quoted text clipped - 28 lines]
> >
> > Pieter
Ross Presser - 14 Jul 2005 16:33 GMT
> Well, I expect it to return DBNull, and not an error! DBNull is actually
> also some kind of value, so throwing an exception isn't really a nice thing
> in my opinion, hehe :-)

DBNull is a kind of value.  But it is not convertible to int, string, or
anything else.  The declaration of the properties are that they return the
types given. An example from one of my typed datasets:

       Public Property label As String
           Get
               Try
                   Return CType(Me(Me.tableVariablepg.labelColumn),String)
               Catch e As InvalidCastException
                   Throw New StrongTypingException( _
                       "Cannot get value because it is DBNull.", e)
               End Try
           End Get
           Set
               Me(Me.tableVariablepg.labelColumn) = value
           End Set
       End Property

Now, if the label property must return a string, then you can't return
DBNull.Value.  Just try it in code - the compiler won't even let you.
Insert "return dbnull.value" below the catch statement, and watch the nice
wiggly blue line appear underneath it.

> When using the Untyped DataSet it returns DBNull
> too, so i don't see a reason why they have changed this with a Typed
> DataSet...

When you use an untyped dataset, it returns an Object type. When you use a
typed dataset, it returns a type.  Simple.

What are you going to do in the code when the value is null? You're going
to do something special. So why not check IsNull() beforehand anyway?
Miha Markic [MVP C#] - 14 Jul 2005 14:38 GMT
Hi Dragu,

Every nullable column in typed dataset has an Is[ColumnName]Null() method.

Signature

Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

> Hi,
>
[quoted text clipped - 17 lines]
>
> Pieter
DraguVaso - 14 Jul 2005 14:42 GMT
Thanks! That solves my problem I guess :-)

> Hi Dragu,
>
[quoted text clipped - 21 lines]
> >
> > Pieter

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.