Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > conversion from type 'DBNull' to type 'float' is not valid

Reply
Thread Tools

conversion from type 'DBNull' to type 'float' is not valid

 
 
ibiza
Guest
Posts: n/a
 
      01-27-2006
Hi all,

I'm using a business logic layer as described in this source code :

http://www.asp.net/QuickStart/util/s...ng=C%23+Source

It works very well until I encountered a small problem recently. When
it is time to bind the data from the db to my class representing an
"author" (let's say, for simplicity), what about if my row["au_state"]
was instead : row["au_idstate"] and was returning a NULL value. In my
"Authors class", I'd represent the idstate with an integer instead of a
string. What must I do when I try to assign a null value to an integer
(more precisely, a float in my case) because for now, I get a nasty
error "conversion from type 'DBNull' to type 'float' is not valid.

Then in my gridview, I want to see a blank cell if my float var is
NULL; not a 0 or something else...

Thanks!

ibiza

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      01-27-2006
Hi,

you'd need to use some sort of default value suitable for the scenario
(maybe 0 with integers?) since as you noted, they cannot represent null
coming from db (DbNull instance). And when such field is put visible on the
UI, you'd need to set it to empty string in case when value represents this
default value (which points that null came from db)

You could create a small library for yourself to deal with it such that
testing for null (with Convert.IsDBNull etc) wouldn't require repetitive
coding.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"ibiza" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi all,
>
> I'm using a business logic layer as described in this source code :
>
> http://www.asp.net/QuickStart/util/s...ng=C%23+Source
>
> It works very well until I encountered a small problem recently. When
> it is time to bind the data from the db to my class representing an
> "author" (let's say, for simplicity), what about if my row["au_state"]
> was instead : row["au_idstate"] and was returning a NULL value. In my
> "Authors class", I'd represent the idstate with an integer instead of a
> string. What must I do when I try to assign a null value to an integer
> (more precisely, a float in my case) because for now, I get a nasty
> error "conversion from type 'DBNull' to type 'float' is not valid.
>
> Then in my gridview, I want to see a blank cell if my float var is
> NULL; not a 0 or something else...
>
> Thanks!
>
> ibiza
>



 
Reply With Quote
 
 
 
 
ibiza
Guest
Posts: n/a
 
      01-27-2006
well, I figured that if I'd put (isdbnull(row["au_idstate"]? null :
row["au_idstate"]))

(in fact : IIf(IsDbNull(row("au_idstate"), Nothing, row("au_idstate"))
in VB.NET)

instead of just row("au_idstate"), I don't get the error anymore and
everything works fine again. The only problem to me is that it binds
nasty "0" in my gridview instead of a nice blank cell....any idea for
that?

Thank a lot!

ibiza

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 from type 'DBNull' to type 'String' is not valid.,asp.net&vb jerryantony Software 0 11-04-2009 08:00 AM
Conversion from type 'DBNull' to type 'String' is not valid Cirene ASP .Net 2 06-19-2008 05:18 AM
Conversion from string to type double is not valid Ed Dror ASP .Net 2 04-08-2008 06:36 PM
Conversion from type 'DBNull' to type 'String' is not valid Chris ASP .Net 2 05-11-2006 08:20 AM
Cast from type 'DBNull' to type 'String' is not valid. Elmo Watson ASP .Net 3 12-25-2003 03:30 AM



Advertisments