How do you handle nullable strings

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

    How do you handle nullable strings

    Can you handle nullable string vb.net 2.0?

    You can with Integer but I get an error when I try to do a

    Dim s as Nullable (Of String)

    Now strings are a nullable type but it can't handle:

    fn.FormName = dbReader("FormN ame")

    You get an error saying:

    Unable to cast object of type 'System.DBNull' to type 'System.String' .

    So do I still need to test for null and set to "" or null if this is the
    case?

    Thanks,

    Tom


  • Herfried K. Wagner [MVP]

    #2
    Re: How do you handle nullable strings

    "tshad" <tshad@dslextre me.comschrieb:
    Can you handle nullable string vb.net 2.0?
    >
    You can with Integer but I get an error when I try to do a
    >
    Dim s as Nullable (Of String)
    >
    Now strings are a nullable type but it can't handle:
    >
    fn.FormName = dbReader("FormN ame")
    >
    You get an error saying:
    >
    Unable to cast object of type 'System.DBNull' to type 'System.String' .
    \\\
    If dbReader(...) Is DBNull.Value Then
    ...
    Else
    ... = DirectCast(dbRe ader(...), String)
    End If
    ///

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • Cor Ligthert[MVP]

      #3
      Re: How do you handle nullable strings

      Tshad,

      A non initialized value in memory has not much to do with an empty field in
      a database.

      DBNull tells that there is actual nothing in the DataBase field,

      Nothing tells that there is no refererence (and therefore as well with a
      strings)

      Null tells that a value field is not even been initialized to its default
      (which is the standard behaviour of VB).

      Nothing when initialized at values (including strings) tells that it is the
      default value.

      Cor

      Comment

      • Andrew Morton

        #4
        Re: How do you handle nullable strings

        tshad wrote:
        Can you handle nullable string vb.net 2.0?
        >
        You can with Integer but I get an error when I try to do a
        >
        Dim s as Nullable (Of String)
        >
        Now strings are a nullable type but it can't handle:
        >
        fn.FormName = dbReader("FormN ame")
        >
        You get an error saying:
        >
        Unable to cast object of type 'System.DBNull' to type 'System.String' .
        >
        So do I still need to test for null and set to "" or null if this is
        the case?
        Side-stepping the problem, in the SQL query you could use
        coalesce(dbFiel dName, '') to get the DB to convert nulls to empty strings.

        Andrew


        Comment

        • rowe_newsgroups

          #5
          Re: How do you handle nullable strings

          On Feb 19, 5:24 pm, "tshad" <ts...@dslextre me.comwrote:
          Can you handle nullable string vb.net 2.0?
          >
          You can with Integer but I get an error when I try to do a
          >
          Dim s as Nullable (Of String)
          >
          Now strings are a nullable type but it can't handle:
          >
          fn.FormName = dbReader("FormN ame")
          >
          You get an error saying:
          >
          Unable to cast object of type 'System.DBNull' to type 'System.String' .
          >
          So do I still need to test for null and set to "" or null if this is the
          case?
          >
          Thanks,
          >
          Tom
          IIRC you can use .ToString() when returning and indexed column from a
          datareader.

          i.e.

          ///////////////
          fn.FormName = dbReader("FormN ame").ToString( )
          //////////////

          should convert that DbNull into a blank string.

          Thanks,

          Seth Rowe [MVP]

          Comment

          • Scott M.

            #6
            Re: How do you handle nullable strings

            IIRC you can use .ToString() when returning and indexed column from a
            datareader.
            >
            i.e.
            >
            ///////////////
            fn.FormName = dbReader("FormN ame").ToString( )
            //////////////
            >
            should convert that DbNull into a blank string.
            >
            Thanks,
            >
            Seth Rowe [MVP]

            I don't believe that works.


            Comment

            • rowe_newsgroups

              #7
              Re: How do you handle nullable strings

              On Feb 20, 11:07 am, "Scott M." <s...@nospam.no spamwrote:
              IIRC you can use .ToString() when returning and indexed column from a
              datareader.
              >
              i.e.
              >
              ///////////////
              fn.FormName = dbReader("FormN ame").ToString( )
              //////////////
              >
              should convert that DbNull into a blank string.
              >
              Thanks,
              >
              Seth Rowe [MVP]
              >
              I don't believe that works.
              That is odd, since that's what I use in my production code.

              Thanks,

              Seth Rowe [MVP]

              Comment

              • Steve Gerrard

                #8
                Re: How do you handle nullable strings

                rowe_newsgroups wrote:
                On Feb 20, 11:07 am, "Scott M." <s...@nospam.no spamwrote:
                >>IIRC you can use .ToString() when returning and indexed column from
                >>a datareader.
                >>
                >>i.e.
                >>
                >>///////////////
                >>fn.FormName = dbReader("FormN ame").ToString( )
                >>//////////////
                >>
                >>should convert that DbNull into a blank string.
                >>
                >>Thanks,
                >>
                >>Seth Rowe [MVP]
                >>
                >I don't believe that works.
                >
                That is odd, since that's what I use in my production code.
                >
                Sure it works.

                System.DBNull.V alue.ToString

                returns a zero length string.

                ToString is not a type converter, it always returns a string - "A String that
                represents the current Object"



                Comment

                Working...