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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elmo Watson

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

    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.V alue or sItem = "" then
    FixLegal=""
    else
    FixLegal="<i>" & sPrefix & ":</i> " & sItem
    End If

    End Function

    Then, in my DataList:

    <%# FixLegal(Contai ner.DataItem("P rpSection"), "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.V alue was supposed to take care of this anomaly

    Any ideas?




  • Scott M.

    #2
    Re: Cast from type 'DBNull' to type 'String' is not valid.

    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" <sputnik75043@n ospamYahoo.com> wrote in message
    news:eJ2ptbkyDH A.1616@TK2MSFTN GP11.phx.gbl...[color=blue]
    > I've got a 'helper' function created, to modify the showing of database
    > results:
    >
    >
    > Then, in my DataList:
    >
    > <%# FixLegal(Contai ner.DataItem("P rpSection"), "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.V alue was supposed to take care of this anomaly
    >
    > Any ideas?
    >
    >
    >
    >[/color]


    Comment

    • bruce barker

      #3
      Re: Cast from type 'DBNull' to type 'String' is not valid.

      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.V alue then
      FixLegal=""
      elseif sItem = "" then
      FixLegal=""
      else
      FixLegal="<i>" & sPrefix & ":</i> " & sItem
      End If

      End Function




      "Scott M." <s-mar@BADSPAMsnet .net> wrote in message
      news:uWr7stkyDH A.3436@tk2msftn gp13.phx.gbl...[color=blue]
      > 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[/color]
      function[color=blue]
      > return data type was.
      >
      > "Elmo Watson" <sputnik75043@n ospamYahoo.com> wrote in message
      > news:eJ2ptbkyDH A.1616@TK2MSFTN GP11.phx.gbl...[color=green]
      > > I've got a 'helper' function created, to modify the showing of database
      > > results:
      > >
      > >
      > > Then, in my DataList:
      > >
      > > <%# FixLegal(Contai ner.DataItem("P rpSection"), "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.V alue was supposed to take care of this anomaly
      > >
      > > Any ideas?
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Scott M.

        #4
        Re: Cast from type 'DBNull' to type 'String' is not valid.

        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" <nospam_brubar@ safeco.com> wrote in message
        news:%23GLqBamy DHA.3216@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > 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.V alue then
        > FixLegal=""
        > elseif sItem = "" then
        > FixLegal=""
        > else
        > FixLegal="<i>" & sPrefix & ":</i> " & sItem
        > End If
        >
        > End Function
        >
        >
        >
        >
        > "Scott M." <s-mar@BADSPAMsnet .net> wrote in message
        > news:uWr7stkyDH A.3436@tk2msftn gp13.phx.gbl...[color=green]
        > > 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[/color]
        > function[color=green]
        > > return data type was.
        > >
        > > "Elmo Watson" <sputnik75043@n ospamYahoo.com> wrote in message
        > > news:eJ2ptbkyDH A.1616@TK2MSFTN GP11.phx.gbl...[color=darkred]
        > > > I've got a 'helper' function created, to modify the showing of[/color][/color][/color]
        database[color=blue][color=green][color=darkred]
        > > > results:
        > > >
        > > >
        > > > Then, in my DataList:
        > > >
        > > > <%# FixLegal(Contai ner.DataItem("P rpSection"), "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.V alue was supposed to take care of this anomaly
        > > >
        > > > Any ideas?
        > > >
        > > >
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...