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
"shapper" wrote:
> On Oct 12, 5:11 pm, gnewsgroup <gnewsgr...@gmail.com> wrote:
> > On Oct 12, 12:06 pm, shapper <mdmo...@gmail.com> wrote:
> >
> >
> >
> > > 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
> >
> > 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.
>
> 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
>
>