How Can an Integer Contain a Null Value?

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

    How Can an Integer Contain a Null Value?

    What does it mean for an integer to have a null value? I am trying to use
    the DataView.Find method. That method has an integer return type which
    contains the "index of the row in the DataView containing the sort key value
    specified; otherwise a null value if the sort key value does not exist."

    By "null value", does it mean System.DBNull? (I thought only objects could
    evaluate to System.DBNull.) How can I test whether an integer variable holds
    a null value?


    -TC


  • Cor

    #2
    Re: How Can an Integer Contain a Null Value?

    Hi TC,[color=blue]
    > What does it mean for an integer to have a null value? I am trying to use
    > the DataView.Find method. That method has an integer return type which
    > contains the "index of the row in the DataView containing the sort key[/color]
    value[color=blue]
    > specified; otherwise a null value if the sort key value does not exist."
    >
    > By "null value", does it mean System.DBNull? (I thought only objects could
    > evaluate to System.DBNull.) How can I test whether an integer variable[/color]
    holds[color=blue]
    > a null value?[/color]

    My first thought of the answer was a value can never be "nothing" or
    "dbnull".

    (This is dangerous stuff, because I once had an argue here about what is
    difficult about English language and then I said by instance the definition
    of words like null.)

    So don't look to much on the words I use, just at the meaning.

    But then I saw that you are probably talking about a database integer, what
    is a total different thing. It is a placeholder for an integer. That can be
    empty or dbnull

    Just beneath your message you see a message with executeScalar.

    Sthephany did send yesterday an example for drygast how to use that. I saw
    it yesterday too the first time for the answer beneath I don't know if it
    fit. For you I think it does.

    I pasted it in beneath

    \\\
    You are using a reader and that will have a record if it exist and it will
    have no records if it does not exist. Your issue is with the fact that you
    are not handling the situation when the reader has no records.

    Change track a little and use ExecuteScalar() against the command object
    with the sql changed to:

    select count(*) from [order] where ordernummer=" & txtOrdernummer. Text

    The result will be 1 if the order exists and 0 if it does not.

    So the test becomes:

    If cmd.ExecuteScal ar() = 0 Then
    ' Order does not exist
    Else
    ' Order does exist
    End If
    ///
    I hope this helps a little bit.

    Cor


    Comment

    • Armin Zingler

      #3
      Re: How Can an Integer Contain a Null Value?

      "TC" <q@w.e> schrieb[color=blue]
      > What does it mean for an integer to have a null value? I am trying to
      > use the DataView.Find method. That method has an integer return type
      > which contains the "index of the row in the DataView containing the
      > sort key value specified; otherwise a null value if the sort key
      > value does not exist."
      >
      > By "null value", does it mean System.DBNull? (I thought only objects
      > could evaluate to System.DBNull.) How can I test whether an integer
      > variable holds a null value?[/color]

      The word "null" can have two different meanings depending on the context. In
      the Framework and for the CLR (docs), Null is the term for "no reference".
      The equivalent term in VB.NET is Nothing. When talking about "Null" in a
      database, it means DBNull (resp. DBNull.Value). It might get confusing when
      you get a message from the framework that deals with database stuff. In this
      case, we have to be clever enough to recognize what is meant by the word
      "Null".

      Talking in the VB.NET context: An Integer variable can neither be Nothing
      nor it can be DBNull. (in a C# context I'd have to say: An Integer variable
      can neither be Null nor it can be DBNull). Whenever the variable is related
      to a database value and it can be DBNull or an Integer, the type must be
      "Object". Only variables declared as Object can either contain DBNull or a
      (boxed) Integer. You can not set an Integer variable to DBNull (you could
      set it to Nothing, but that's equal to setting it to zero (and actually it
      should not be allowed because it's only confusing)). If the variable is
      declared as Object, you can use

      If theValue Is DBNull.VAlue then
      msgbox "database field contains Null"
      else
      msgbox theValue
      end if

      The first msgbox could also show
      msgbox "database field contains DBNull"
      but in the given context ("database field...") it's sufficient to show
      "Null" in the message.


      --
      Armin

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: How Can an Integer Contain a Null Value?

        TC,
        In addition to the others comments.

        According to David Sceppa's book "Microsoft ADO.NET - Core Reference" from
        MS Press, DataView.Find returns -1 if the desired row is not found. Which is
        consistent with other similar searching type methods found in the framework.

        Running a quick test confirms that DataView.Find returns a -1 when the key
        is not found.

        If you are using ADO.NET, I would recommend David's book.

        Hope this helps
        Jay

        "TC" <q@w.e> wrote in message news:W5Mib.4869 $iq3.1202@okepr ead01...[color=blue]
        > What does it mean for an integer to have a null value? I am trying to use
        > the DataView.Find method. That method has an integer return type which
        > contains the "index of the row in the DataView containing the sort key[/color]
        value[color=blue]
        > specified; otherwise a null value if the sort key value does not exist."
        >
        > By "null value", does it mean System.DBNull? (I thought only objects could
        > evaluate to System.DBNull.) How can I test whether an integer variable[/color]
        holds[color=blue]
        > a null value?
        >
        >
        > -TC
        >
        >[/color]


        Comment

        • TC

          #5
          Re: How Can an Integer Contain a Null Value?

          Cor,

          Thank you for the reply. In fact, I was asking about a system integer, not a
          database integer. It seems that the problem arises from incorrect/ambiguous
          syntax in the VB.NET documentation. Instead of saying the Find method
          returns an integer "null value", the documentation should say it returns the
          value -1.

          -TC


          "Cor" <non@non.com> wrote in message
          news:3f8b9ae6$0 $19185$48b97d01 @reader20.wxs.n l...[color=blue]
          > Hi TC,[color=green]
          > > What does it mean for an integer to have a null value? I am trying to[/color][/color]
          use[color=blue][color=green]
          > > the DataView.Find method. That method has an integer return type which
          > > contains the "index of the row in the DataView containing the sort key[/color]
          > value[color=green]
          > > specified; otherwise a null value if the sort key value does not exist."
          > >
          > > By "null value", does it mean System.DBNull? (I thought only objects[/color][/color]
          could[color=blue][color=green]
          > > evaluate to System.DBNull.) How can I test whether an integer variable[/color]
          > holds[color=green]
          > > a null value?[/color]
          >
          > My first thought of the answer was a value can never be "nothing" or
          > "dbnull".
          >
          > (This is dangerous stuff, because I once had an argue here about what is
          > difficult about English language and then I said by instance the[/color]
          definition[color=blue]
          > of words like null.)
          >
          > So don't look to much on the words I use, just at the meaning.
          >
          > But then I saw that you are probably talking about a database integer,[/color]
          what[color=blue]
          > is a total different thing. It is a placeholder for an integer. That can[/color]
          be[color=blue]
          > empty or dbnull
          >
          > Just beneath your message you see a message with executeScalar.
          >
          > Sthephany did send yesterday an example for drygast how to use that. I saw
          > it yesterday too the first time for the answer beneath I don't know if it
          > fit. For you I think it does.
          >
          > I pasted it in beneath
          >
          > \\\
          > You are using a reader and that will have a record if it exist and it will
          > have no records if it does not exist. Your issue is with the fact that you
          > are not handling the situation when the reader has no records.
          >
          > Change track a little and use ExecuteScalar() against the command object
          > with the sql changed to:
          >
          > select count(*) from [order] where ordernummer=" & txtOrdernummer. Text
          >
          > The result will be 1 if the order exists and 0 if it does not.
          >
          > So the test becomes:
          >
          > If cmd.ExecuteScal ar() = 0 Then
          > ' Order does not exist
          > Else
          > ' Order does exist
          > End If
          > ///
          > I hope this helps a little bit.
          >
          > Cor[/color]


          Comment

          • TC

            #6
            Re: How Can an Integer Contain a Null Value?

            Armin,
            [color=blue]
            > The word "null" can have two different meanings depending on the[/color]
            context...

            It seems that the word "null" can have a third meaning too. In the online
            documentation for the TreeView.Find method, "null value" apparently means
            the integer value -1.

            -TC


            Comment

            • TC

              #7
              Re: How Can an Integer Contain a Null Value?

              Jay,

              Thank you. That solves the mystery.

              -TC


              "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
              news:%23BhFHFmk DHA.2676@TK2MSF TNGP11.phx.gbl. ..[color=blue]
              > TC,
              > In addition to the others comments.
              >
              > According to David Sceppa's book "Microsoft ADO.NET - Core Reference" from
              > MS Press, DataView.Find returns -1 if the desired row is not found. Which[/color]
              is[color=blue]
              > consistent with other similar searching type methods found in the[/color]
              framework.[color=blue]
              >
              > Running a quick test confirms that DataView.Find returns a -1 when the key
              > is not found.
              >
              > If you are using ADO.NET, I would recommend David's book.
              >
              > Hope this helps
              > Jay
              >
              > "TC" <q@w.e> wrote in message news:W5Mib.4869 $iq3.1202@okepr ead01...[color=green]
              > > What does it mean for an integer to have a null value? I am trying to[/color][/color]
              use[color=blue][color=green]
              > > the DataView.Find method. That method has an integer return type which
              > > contains the "index of the row in the DataView containing the sort key[/color]
              > value[color=green]
              > > specified; otherwise a null value if the sort key value does not exist."
              > >
              > > By "null value", does it mean System.DBNull? (I thought only objects[/color][/color]
              could[color=blue][color=green]
              > > evaluate to System.DBNull.) How can I test whether an integer variable[/color]
              > holds[color=green]
              > > a null value?
              > >
              > >
              > > -TC[/color][/color]


              Comment

              • Armin Zingler

                #8
                Re: How Can an Integer Contain a Null Value?

                "TC" <q@w.e> schrieb[color=blue]
                > Armin,
                >[color=green]
                > > The word "null" can have two different meanings depending on the[/color]
                > context...
                >
                > It seems that the word "null" can have a third meaning too. In the
                > online documentation for the TreeView.Find method, "null value"
                > apparently means the integer value -1.[/color]

                To me it seems to be a documentation fault.


                --
                Armin

                Comment

                • Luca Minudel

                  #9
                  ...with NullableTypes


                  Actually the .NET Framework don't let you set Null
                  (Nothing in VB) to an int, a bool, a DateTime, etc.

                  So BCL functions cannot return Null value for built-in
                  types.

                  If you need to set Null (Nothing in VB) to an int, a bool,
                  a DateTime, ... you can use NullableTypes:
                  open source, source code, freeware, shareware, programmer, developer, VB, Visual Basic, C++, C#, C Sharp, Oracle, Oracle8i, Oracle9i, VB 6, VB6, VB.NET, Visual Basic.NET, Visual Basic 6, Visual Studio, Visual Studio.NET, framework .net, dotNet, microsoft, oracle, OO, OOA, OOD, OOP, UML, unified modeling language


                  bye (luKa)


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: ...with NullableTypes

                    Luca,[color=blue]
                    > Actually the .NET Framework don't let you set Null
                    > (Nothing in VB) to an int, a bool, a DateTime, etc.[/color]
                    However! VB.NET treats Nothing as the default value for any type.

                    <blockquote>
                    Nothing is a special literal; it does not have a type and is convertible to
                    all types in the type system. When converted to a particular type, it is the
                    equivalent of the default value of that type.
                    </blockquote>



                    In other words, if you assign Nothing to an Integer that integer is set to
                    the 'default' value for an Integer which is Zero!

                    Try it!
                    Dim i as Integer = Nothing
                    Dim b As Boolean = Nothing
                    Dim c As Char = Nothing
                    Dim pt As Point = Nothing

                    If you run the above you will find each value has the defaults for that
                    value. (0, False, Char.MinValue (which the debugger displays as Nothing)).
                    Seeing as Point is a structure, the default for Point is the default for
                    each of its members, in other words pt.x = 0 & pt.y = 0.

                    Hope this helps
                    Jay


                    "Luca Minudel" <anonymous@disc ussions.microso ft.com> wrote in message
                    news:2b57d01c39 30b$ed903480$a6 01280a@phx.gbl. ..[color=blue]
                    >
                    > Actually the .NET Framework don't let you set Null
                    > (Nothing in VB) to an int, a bool, a DateTime, etc.
                    >
                    > So BCL functions cannot return Null value for built-in
                    > types.
                    >
                    > If you need to set Null (Nothing in VB) to an int, a bool,
                    > a DateTime, ... you can use NullableTypes:
                    > http://nullabletypes.sourceforge.net/
                    >
                    > bye (luKa)
                    >
                    >[/color]


                    Comment

                    • Luca Minudel

                      #11
                      Re: ...with NullableTypes

                      [color=blue]
                      >-----Original Message-----
                      >Luca,[color=green]
                      >> Actually the .NET Framework don't let you set Null
                      >> (Nothing in VB) to an int, a bool, a DateTime, etc.[/color]
                      >However! VB.NET treats Nothing as the default value for[/color]
                      any type.[color=blue]
                      >
                      >Hope this helps
                      >Jay
                      >
                      >
                      >"Luca Minudel" <anonymous@disc ussions.microso ft.com> wrote[color=green]
                      >>
                      >> If you need to set Null (Nothing in VB) to an int, a bool,
                      >> a DateTime, ... you can use NullableTypes:
                      >> http://nullabletypes.sourceforge.net/
                      >>[/color][/color]

                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: ...with NullableTypes

                        Luca,
                        ??

                        Did you have something to say? As all I see is an empty trimmed message!

                        Thanks
                        Jay

                        "Luca Minudel" <anonymous@disc ussions.microso ft.com> wrote in message
                        news:352301c393 4f$3c20d530$350 1280a@phx.gbl.. .[color=blue]
                        >[color=green]
                        > >-----Original Message-----
                        > >Luca,[color=darkred]
                        > >> Actually the .NET Framework don't let you set Null
                        > >> (Nothing in VB) to an int, a bool, a DateTime, etc.[/color]
                        > >However! VB.NET treats Nothing as the default value for[/color]
                        > any type.[color=green]
                        > >
                        > >Hope this helps
                        > >Jay
                        > >
                        > >
                        > >"Luca Minudel" <anonymous@disc ussions.microso ft.com> wrote[color=darkred]
                        > >>
                        > >> If you need to set Null (Nothing in VB) to an int, a bool,
                        > >> a DateTime, ... you can use NullableTypes:
                        > >> http://nullabletypes.sourceforge.net/
                        > >>[/color][/color]
                        >[/color]


                        Comment

                        • Guest's Avatar

                          #13
                          Re: ...with NullableTypes

                          Here it is! -

                          Yes this help! I'm primarely a C# programmer so I made
                          some confusion about Nothing in VB.

                          What I meant is that NullableTypes let you set Null to an
                          int, a bool, a DateTime and to quite any built-in .NET
                          type. But Null is not the default value and it is not
                          Nothing. It's a different value and it is strong typed
                          (i.e. Null for a DateTime is different from a Null for a
                          bool).

                          I will check the documentation of NullableTypes to see if
                          I stated something confusing about Nothing. If you will
                          have the opportunity to see the NullableTypes
                          documentation please let me know if you find something
                          confusing for VB.NET programmers.


                          Thanks, (luKa)

                          [color=blue]
                          >-----Original Message-----
                          >Did you have something to say? As all I see is an empty[/color]
                          trimmed message![color=blue]
                          >
                          >"Luca Minudel" <anonymous@disc ussions.microso ft.com>[/color]
                          wrote in message[color=blue]
                          >news:352301c39 34f$3c20d530$35 01280a@phx.gbl. ..[color=green]
                          >>[color=darkred]
                          >> >-----Original Message-----
                          >> >Luca,
                          >> >> Actually the .NET Framework don't let you set Null
                          >> >> (Nothing in VB) to an int, a bool, a DateTime, etc.
                          >> >However! VB.NET treats Nothing as the default value for[/color]
                          >> any type.[color=darkred]
                          >> >
                          >> >Hope this helps
                          >> >Jay
                          >> >
                          >> >
                          >> >"Luca Minudel" <anonymous@disc ussions.microso ft.com>[/color][/color][/color]
                          wrote[color=blue][color=green][color=darkred]
                          >> >>
                          >> >> If you need to set Null (Nothing in VB) to an int, a[/color][/color][/color]
                          bool,[color=blue][color=green][color=darkred]
                          >> >> a DateTime, ... you can use NullableTypes:
                          >> >> http://nullabletypes.sourceforge.net/
                          >> >>[/color][/color][/color]

                          Comment

                          • Luca Minudel

                            #14
                            Re: ...with NullableTypes

                            Here it is! -

                            Yes this help! I'm primarely a C# programmer so I made
                            some confusion about Nothing in VB.

                            What I meant is that NullableTypes let you set Null to an
                            int, a bool, a DateTime and to quite any built-in .NET
                            type. But Null is not the default value and it is not
                            Nothing. It's a different value and it is strong typed
                            (i.e. Null for a DateTime is different from a Null for a
                            bool).

                            If you will have the opportunity to see the NullableTypes
                            documentation please let me know if you find something
                            confusing for VB.NET programmers.

                            Thanks, (luKa)

                            [color=blue]
                            >-----Original Message-----
                            >Luca,
                            >??
                            >
                            >Did you have something to say? As all I see is an empty[/color]
                            trimmed message![color=blue]
                            >
                            >Thanks
                            >Jay
                            >
                            >"Luca Minudel" <anonymous@disc ussions.microso ft.com>[/color]
                            wrote in message[color=blue]
                            >news:352301c39 34f$3c20d530$35 01280a@phx.gbl. ..[color=green]
                            >>[color=darkred]
                            >> >-----Original Message-----
                            >> >Luca,
                            >> >> Actually the .NET Framework don't let you set Null
                            >> >> (Nothing in VB) to an int, a bool, a DateTime, etc.
                            >> >However! VB.NET treats Nothing as the default value for[/color]
                            >> any type.[color=darkred]
                            >> >
                            >> >Hope this helps
                            >> >Jay
                            >> >
                            >> >
                            >> >"Luca Minudel" <anonymous@disc ussions.microso ft.com>[/color][/color][/color]
                            wrote[color=blue][color=green][color=darkred]
                            >> >>
                            >> >> If you need to set Null (Nothing in VB) to an int, a[/color][/color][/color]
                            bool,[color=blue][color=green][color=darkred]
                            >> >> a DateTime, ... you can use NullableTypes:
                            >> >> http://nullabletypes.sourceforge.net/
                            >> >>[/color]
                            >>[/color][/color]

                            Comment

                            • Jay B. Harlow [MVP - Outlook]

                              #15
                              Re: ...with NullableTypes

                              Luca,
                              I follow you, your other post was just a little terse, I missed what you
                              were attempting to say.

                              NullableTypes allows the concept of a DB Null.

                              I was not talking about the concept of a DB Null, so I missed your
                              connection.

                              Thanks
                              Jay

                              "Luca Minudel" <anonymous@disc ussions.microso ft.com> wrote in message
                              news:04c901c393 e6$07b21ba0$a00 1280a@phx.gbl.. .[color=blue]
                              > Here it is! -
                              >
                              > Yes this help! I'm primarely a C# programmer so I made
                              > some confusion about Nothing in VB.
                              >
                              > What I meant is that NullableTypes let you set Null to an
                              > int, a bool, a DateTime and to quite any built-in .NET
                              > type. But Null is not the default value and it is not
                              > Nothing. It's a different value and it is strong typed
                              > (i.e. Null for a DateTime is different from a Null for a
                              > bool).
                              >
                              > If you will have the opportunity to see the NullableTypes
                              > documentation please let me know if you find something
                              > confusing for VB.NET programmers.
                              >
                              > Thanks, (luKa)
                              >
                              >[color=green]
                              > >-----Original Message-----
                              > >Luca,
                              > >??
                              > >
                              > >Did you have something to say? As all I see is an empty[/color]
                              > trimmed message![color=green]
                              > >
                              > >Thanks
                              > >Jay
                              > >
                              > >"Luca Minudel" <anonymous@disc ussions.microso ft.com>[/color]
                              > wrote in message[color=green]
                              > >news:352301c39 34f$3c20d530$35 01280a@phx.gbl. ..[color=darkred]
                              > >>
                              > >> >-----Original Message-----
                              > >> >Luca,
                              > >> >> Actually the .NET Framework don't let you set Null
                              > >> >> (Nothing in VB) to an int, a bool, a DateTime, etc.
                              > >> >However! VB.NET treats Nothing as the default value for
                              > >> any type.
                              > >> >
                              > >> >Hope this helps
                              > >> >Jay
                              > >> >
                              > >> >
                              > >> >"Luca Minudel" <anonymous@disc ussions.microso ft.com>[/color][/color]
                              > wrote[color=green][color=darkred]
                              > >> >>
                              > >> >> If you need to set Null (Nothing in VB) to an int, a[/color][/color]
                              > bool,[color=green][color=darkred]
                              > >> >> a DateTime, ... you can use NullableTypes:
                              > >> >> http://nullabletypes.sourceforge.net/
                              > >> >>
                              > >>[/color][/color]
                              >[/color]


                              Comment

                              Working...