Cast from type 'DBNull' to type 'String' is not valid.

Discussion in 'ASP .Net' started by Elmo Watson, Dec 24, 2003.

  1. Elmo Watson

    Elmo Watson Guest

    I've got a 'helper' function created, to modify the showing of database
    results:

    Function FixLegal(sItem as String, sPrefix as String)
    if sItem is System.DBNull.Value or sItem = "" then
    FixLegal=""
    else
    FixLegal="<i>" & sPrefix & ":</i> " & sItem
    End If

    End Function

    Then, in my DataList:

    <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>

    This works great, unless there's a Null Value in the table for that
    field - - when the value is Null - - I get the error in the subject. I
    THOUGHT System.DBNull.Value was supposed to take care of this anomaly

    Any ideas?
    Elmo Watson, Dec 24, 2003
    #1
    1. Advertising

  2. Elmo Watson

    Scott M. Guest

    Try changing your code to read like this:

    Function FixLegal(sItem as String, sPrefix as String) As String
    if IsDBNull(sItem) or sItem = "" then
    Return ""
    else
    Return "<i>" & sPrefix & ":</i> " & sItem
    End If
    End Function

    **Note that in your original function you never indicated what the function
    return data type was.

    "Elmo Watson" <> wrote in message
    news:...
    > I've got a 'helper' function created, to modify the showing of database
    > results:
    >
    >
    > Then, in my DataList:
    >
    > <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>
    >
    > This works great, unless there's a Null Value in the table for that
    > field - - when the value is Null - - I get the error in the subject. I
    > THOUGHT System.DBNull.Value was supposed to take care of this anomaly
    >
    > Any ideas?
    >
    >
    >
    >
    Scott M., Dec 24, 2003
    #2
    1. Advertising

  3. Elmo Watson

    bruce barker Guest

    this will still fail as vb does not short cut if's , try:


    Function FixLegal(sItem as String, sPrefix as String)
    if sItem is System.DBNull.Value then
    FixLegal=""
    elseif sItem = "" then
    FixLegal=""
    else
    FixLegal="<i>" & sPrefix & ":</i> " & sItem
    End If

    End Function




    "Scott M." <> wrote in message
    news:...
    > Try changing your code to read like this:
    >
    > Function FixLegal(sItem as String, sPrefix as String) As String
    > if IsDBNull(sItem) or sItem = "" then
    > Return ""
    > else
    > Return "<i>" & sPrefix & ":</i> " & sItem
    > End If
    > End Function
    >
    > **Note that in your original function you never indicated what the

    function
    > return data type was.
    >
    > "Elmo Watson" <> wrote in message
    > news:...
    > > I've got a 'helper' function created, to modify the showing of database
    > > results:
    > >
    > >
    > > Then, in my DataList:
    > >
    > > <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>
    > >
    > > This works great, unless there's a Null Value in the table for that
    > > field - - when the value is Null - - I get the error in the subject. I
    > > THOUGHT System.DBNull.Value was supposed to take care of this anomaly
    > >
    > > Any ideas?
    > >
    > >
    > >
    > >

    >
    >
    bruce barker, Dec 24, 2003
    #3
  4. Elmo Watson

    Scott M. Guest

    Can you elaborate? Your code does not include a return type for the
    function, has a clause to set sItem to exactly the same value that it has
    during a test (so that elseIf is not needed at all) and doesn't use the
    "Return" syntax.

    But assuming you corrected these things, what is different about your code
    and mine?

    VB does include the OrElse logical operand for shortcutting but it wouldn't
    be needed here.


    "bruce barker" <> wrote in message
    news:%...
    > this will still fail as vb does not short cut if's , try:
    >
    >
    > Function FixLegal(sItem as String, sPrefix as String)
    > if sItem is System.DBNull.Value then
    > FixLegal=""
    > elseif sItem = "" then
    > FixLegal=""
    > else
    > FixLegal="<i>" & sPrefix & ":</i> " & sItem
    > End If
    >
    > End Function
    >
    >
    >
    >
    > "Scott M." <> wrote in message
    > news:...
    > > Try changing your code to read like this:
    > >
    > > Function FixLegal(sItem as String, sPrefix as String) As String
    > > if IsDBNull(sItem) or sItem = "" then
    > > Return ""
    > > else
    > > Return "<i>" & sPrefix & ":</i> " & sItem
    > > End If
    > > End Function
    > >
    > > **Note that in your original function you never indicated what the

    > function
    > > return data type was.
    > >
    > > "Elmo Watson" <> wrote in message
    > > news:...
    > > > I've got a 'helper' function created, to modify the showing of

    database
    > > > results:
    > > >
    > > >
    > > > Then, in my DataList:
    > > >
    > > > <%# FixLegal(Container.DataItem("PrpSection"), "Section") %>
    > > >
    > > > This works great, unless there's a Null Value in the table for that
    > > > field - - when the value is Null - - I get the error in the subject. I
    > > > THOUGHT System.DBNull.Value was supposed to take care of this anomaly
    > > >
    > > > Any ideas?
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Scott M., Dec 25, 2003
    #4
    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. Mike Newton
    Replies:
    0
    Views:
    2,634
    Mike Newton
    Jul 27, 2004
  2. tshad
    Replies:
    6
    Views:
    3,444
    tshad
    Dec 15, 2004
  3. Chris
    Replies:
    2
    Views:
    21,316
    Chris
    May 11, 2006
  4. Cirene
    Replies:
    2
    Views:
    882
    nick chan
    Jun 19, 2008
  5. Rob
    Replies:
    0
    Views:
    341
Loading...

Share This Page