Conversion Problem

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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.
 
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
 
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.
 
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)
 
Back
Top