Null value in Datareader

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

    Null value in Datareader

    Hi Group,
    How can I overcome the issue of null values in a datareader. I keep getting
    the dbnull error when I try to assign a null value to a text box.

    TIA

  • zacks@construction-imaging.com

    #2
    Re: Null value in Datareader

    On May 29, 3:02 pm, "axapta" <jas.jac...@gma il.comwrote:
    Hi Group,
    How can I overcome the issue of null values in a datareader.  I keep getting
    the dbnull error when I try to assign a null value to a text box.
    >
    TIA
    Check out the DataReader IsDBNull Method.

    Comment

    • =?Utf-8?B?U3VydHVyWg==?=

      #3
      Re: Null value in Datareader

      You need to test each and every field in your dataset if it is Null before
      using it.

      This annoyed me so much that I now just make all my database fields
      non-nullable.

      A common solution is to have a "NoNull" function or something that
      automatically changes nulls to zero or empty string.

      --
      David Streeter
      Synchrotech Software
      Sydney Australia


      "zacks@construc tion-imaging.com" wrote:
      On May 29, 3:02 pm, "axapta" <jas.jac...@gma il.comwrote:
      Hi Group,
      How can I overcome the issue of null values in a datareader. I keep getting
      the dbnull error when I try to assign a null value to a text box.

      TIA
      >
      Check out the DataReader IsDBNull Method.
      >

      Comment

      • rowe_newsgroups

        #4
        Re: Null value in Datareader

        On May 29, 3:02 pm, "axapta" <jas.jac...@gma il.comwrote:
        Hi Group,
        How can I overcome the issue of null values in a datareader. I keep getting
        the dbnull error when I try to assign a null value to a text box.
        >
        TIA
        It's actually quite easy, and requires no checks for DbNull if you
        going to a string value:

        //////////////
        If dataReader.Read () Then
        textBox1.Text = dr("MyColumnNam e").ToString ()
        End If
        /////////////

        The ToString() method will happily turn a DbNull into a blank string
        ("") when called. It's a great time saver :-)

        Thanks,

        Seth Rowe [MVP]

        Comment

        • cfps.Christian

          #5
          Re: Null value in Datareader

          I always had to do a check on my datareader values as they came out of
          the database.

          if not dr[0] is DBNull.Value then
          textbox1.text = dr[0].ToString() 'I always have option strict on
          end if

          Kind of a pain, which is why I stopped using null unless I had to.

          Comment

          • rowe_newsgroups

            #6
            Re: Null value in Datareader

            On May 30, 9:53 am, "cfps.Christian " <ge0193...@otc. eduwrote:
            I always had to do a check on my datareader values as they came out of
            the database.
            >
            if not dr[0] is DBNull.Value then
            textbox1.text = dr[0].ToString() 'I always have option strict on
            end if
            >
            Kind of a pain, which is why I stopped using null unless I had to.
            You should be able to just do "textbox1.t ext = dr[0].ToString()" since
            as written above the ToString will convert a dbnull to a blank string.

            Thanks,

            Seth Rowe [MVP]

            Comment

            Working...