Conversion from type 'DBNull' to type 'String' is not valid

Discussion in 'ASP .Net' started by Cirene, Jun 18, 2008.

  1. Cirene

    Cirene Guest

    I'm setting a databound label's backcolor and forecolor like this...

    <asp:Label ID="lblSampleColorScheme" runat="server"

    BackColor='<%#
    System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")) %>'
    Font-Bold="True"

    ForeColor='<%#
    System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) %>'
    Text="CURRENT COLOR SCHEME"></asp:Label>


    The problem is when the value is NULL. I get the above error. How can I
    say, if it's null in the db then use "#ffffff"?

    Thanks!
    Cirene, Jun 18, 2008
    #1
    1. Advertising

  2. Cirene

    Cirene Guest

    This doesn't seem to work...

    <asp:Label ID="lblSampleColorScheme" runat="server"

    BackColor='<%# iif(eval("AttachmentEntityBackColor").equals(DBNull.value),
    "#ffffff", System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")))
    %>' Font-Bold="True"

    ForeColor='<%# iif(eval("AttachmentEntityForeColor").equals(DBNull.value),
    "#000000", System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")))
    %>' Text="CURRENT COLOR SCHEME"></asp:Label>


    "Cirene" <> wrote in message
    news:...
    > I'm setting a databound label's backcolor and forecolor like this...
    >
    > <asp:Label ID="lblSampleColorScheme" runat="server"
    >
    > BackColor='<%#
    > System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")) %>'
    > Font-Bold="True"
    >
    > ForeColor='<%#
    > System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) %>'
    > Text="CURRENT COLOR SCHEME"></asp:Label>
    >
    >
    > The problem is when the value is NULL. I get the above error. How can I
    > say, if it's null in the db then use "#ffffff"?
    >
    > Thanks!
    >
    Cirene, Jun 18, 2008
    #2
    1. Advertising

  3. Cirene

    nick chan Guest

    u can't use IIF()
    System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) will
    still be executed, resulting in error

    (IIF is not equaivalent to C's '?' command)

    u need to write another function

    public function BackColor(AColor as Object) as String
    if typeof AColor is DBNull then
    return "#FFFFFF"
    else
    return System.Drawing.Color.FromName(AColor)
    end if
    end function

    BackColor='<%#BackColor(eval("AttachmentEntityBackColor"))%>'

    On Jun 19, 5:33 am, "Cirene" <> wrote:
    > This doesn't seem to work...
    >
    > <asp:Label ID="lblSampleColorScheme" runat="server"
    >
    > BackColor='<%# iif(eval("AttachmentEntityBackColor").equals(DBNull.value),
    > "#ffffff", System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")))
    > %>' Font-Bold="True"
    >
    > ForeColor='<%# iif(eval("AttachmentEntityForeColor").equals(DBNull.value),
    > "#000000", System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")))
    > %>' Text="CURRENT COLOR SCHEME"></asp:Label>
    >
    > "Cirene" <> wrote in message
    >
    > news:...
    >
    > > I'm setting a databound label's backcolor and forecolor like this...

    >
    > > <asp:Label ID="lblSampleColorScheme" runat="server"

    >
    > > BackColor='<%#
    > > System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")) %>'
    > > Font-Bold="True"

    >
    > > ForeColor='<%#
    > > System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) %>'
    > > Text="CURRENT COLOR SCHEME"></asp:Label>

    >
    > > The problem is when the value is NULL.  I get the above error.  How can I
    > > say, if it's null in the db then use "#ffffff"?

    >
    > > Thanks!
    nick chan, Jun 19, 2008
    #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,044
    Scott M.
    Dec 25, 2003
  2. tshad
    Replies:
    6
    Views:
    3,465
    tshad
    Dec 15, 2004
  3. ibiza
    Replies:
    2
    Views:
    5,347
    ibiza
    Jan 27, 2006
  4. Chris
    Replies:
    2
    Views:
    21,338
    Chris
    May 11, 2006
  5. Rob
    Replies:
    0
    Views:
    356
Loading...

Share This Page