PC Review


Reply
Thread Tools Rate Thread

Conversion Problem

 
 
shapper
Guest
Posts: n/a
 
      12th Oct 2007
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

 
Reply With Quote
 
 
 
 
gnewsgroup
Guest
Posts: n/a
 
      12th Oct 2007
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.

 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      12th Oct 2007
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

 
Reply With Quote
 
gnewsgroup
Guest
Posts: n/a
 
      12th Oct 2007
On Oct 12, 12:15 pm, shapper <mdmo...@gmail.com> 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


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.

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      12th Oct 2007
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
>
>

 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      12th Oct 2007
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)

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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion Problem Roger Microsoft C# .NET 6 17th May 2007 07:10 PM
mdb Conversion problem? =?Utf-8?B?R0hM?= Microsoft Access 1 16th Nov 2004 11:49 AM
Conversion Problem Del Nash Microsoft Access VBA Modules 3 27th May 2004 01:12 PM
MM2: AVI Conversion Problem Brian Windows XP MovieMaker 3 10th Mar 2004 01:21 PM
C# to VB.Net conversion problem? Özden Irmak Microsoft VB .NET 5 13th Dec 2003 01:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:12 AM.