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

Discussion in 'ASP .Net' started by ibiza, Jan 27, 2006.

  1. ibiza

    ibiza Guest

    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
     
    ibiza, Jan 27, 2006
    #1
    1. Advertising

  2. ibiza

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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
    >
     
    Teemu Keiski, Jan 27, 2006
    #2
    1. Advertising

  3. ibiza

    ibiza Guest

    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
     
    ibiza, Jan 27, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elmo Watson
    Replies:
    3
    Views:
    6,049
    Scott M.
    Dec 25, 2003
  2. Guest
    Replies:
    7
    Views:
    9,574
    Scott M.
    Jun 9, 2004
  3. Chris
    Replies:
    2
    Views:
    21,346
    Chris
    May 11, 2006
  4. bd
    Replies:
    0
    Views:
    635
  5. Cirene
    Replies:
    2
    Views:
    904
    nick chan
    Jun 19, 2008
Loading...

Share This Page