Conversion Problem

S

shapper

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
 
G

gnewsgroup

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.
 
S

shapper

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
 
G

gnewsgroup

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

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.
 
B

bruce barker

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top