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

I

ibiza

Hi all,

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

http://www.asp.net/QuickStart/util/...s\App_Code\AuthorsComponent.cs&lang=C#+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
 
T

Teemu Keiski

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

ibiza

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top