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 / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Conversion Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 12 Oct 2007 17:06 GMT
Hi,

I am trying to fill a Generic.List(Of MyClass) with a dataset row
values taken from an MSSQL database:

For Each drPost As DataRow In dsPosts.Tables(0).Rows

  Dim post As New Post

  With post
   .AverageRating = Convert.ToDouble(drPost("PostAverageRating"))
   .IsPublished = Convert.ToBoolean(drPost("PostIsPublished"))
   .NumberOfComments =
Convert.ToInt32(drPost("PostNumberOfComments"))
   .PostId = New Guid(Convert.ToString(drPost("PostId")))
   .Title = Convert.ToString(drPost("PostTitle"))
   .UpdatedDate = Convert.ToDateTime(drPost("PostUpdatedDate"))
  End With

  posts.Add(post)

Next drPost

Some of the rows might have empty fields.

I am having an error right on the first conversion (AverageRating):

Object cannot be cast from DBNull to other types.

What am I doing wrong?

Thanks,

Miguel
gnewsgroup - 12 Oct 2007 17:11 GMT
> Hi,
>
[quoted text clipped - 30 lines]
>
> Miguel

The exception message has told you: Object cannot be cast from DBNull
to other types.  drPost("PostAverageRating") is null, and you cannot
convert a null to a double.  You gotta investigate why
drPost("PostAverageRating") is null.
shapper - 12 Oct 2007 17:15 GMT
> > Hi,
>
[quoted text clipped - 35 lines]
> convert a null to a double.  You gotta investigate why
> drPost("PostAverageRating") is null.

But the point is that some of the records might have null fields. So
if a field is null the correspondent class property would become
Nothing.
Are you saying that for each conversion i need to check if
drPost(.......) is null and make the conversion just after it?

Isn't there a straight forward way to give the Nothing value to class
property when drPost(.......) is null?

Thanks,
Miguel
gnewsgroup - 12 Oct 2007 17:24 GMT
> > > Hi,
>
[quoted text clipped - 47 lines]
> Thanks,
> Miguel

First of all, I think it is suggested that we always check if it is a
null before we attempt to use the value.

Second, converting a null to a Double simply does not happen.
Peter Bromberg [C# MVP] - 12 Oct 2007 17:27 GMT
just test the item for IsDbNull or =DbNull.Value. If it is, you can give the
property say a default value  of zero and no exception will be thrown.
-- Peter
Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> > > Hi,
> >
[quoted text clipped - 47 lines]
> Thanks,
> Miguel
bruce barker - 12 Oct 2007 19:18 GMT
thats correct.

in .net value types like double, int, date, etc, do not support null
(nothing) as value, so a function returning a double, can not return null.

-- bruce (sqlwork.com)

>>> Hi,
>>> I am trying to fill a Generic.List(Of MyClass) with a dataset row
[quoted text clipped - 34 lines]
> Thanks,
> Miguel

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.