handling dbnulls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cm9kY2hhcg==?=

    #1

    handling dbnulls

    hey all,

    if (DataReader["ParentId"].ToString() != String.Empty)
    {
    oCategory.Paren tId = Convert.ToInt32 (DataReader["ParentId"]);
    }

    ParentId has some null values coming from my database. Is this the correct
    way to handle the nulls for this field (or any field for that matter)?

    When i debug, inside the immediate window i type:
    ?DataReader["ParentId"]
    { }

    I'm not sure what the 2 braces mean so i added the ToString() and just
    checked for " ".

    One more thing, about the Convert.ToInt32 is this ok to use? Why isn't there
    just a Convert.ToInteg er?

    thanks,
    rodchar
  • Jon Skeet [C# MVP]

    #2
    Re: handling dbnulls

    On Aug 8, 2:56 pm, rodchar <rodc...@discus sions.microsoft .comwrote:
    hey all,
    >
    if (DataReader["ParentId"].ToString() != String.Empty)
    {
    oCategory.Paren tId = Convert.ToInt32 (DataReader["ParentId"]);
    }
    >
    ParentId has some null values coming from my database. Is this the correct
    way to handle the nulls for this field (or any field for that matter)?
    It's not a terribly nice way of doing it. I'd use

    if (!(DataReader["ParentId"] is DBNull))
    {
    ....
    }
    One more thing, about the Convert.ToInt32 is this ok to use? Why isn't there
    just a Convert.ToInteg er?
    Because the .NET type name is ToInt32. "ToInteger" would be ambiguous
    between Int16, Int32 and Int64 (aka short, int, long in C#).

    Jon

    Comment

    • madhatter2647@gmail.com

      #3
      Re: handling dbnulls

      if (DataReader["ParentId"] != System.DBNull.V alue)

      is the correct syntax

      the 'two braces' are the identifier for the column in the datareader.

      there is no 'convert.tointe ger' function because there are many types
      of integer representations in the framework. Int16, Int32, Int64 for
      signed integers (ie negative and positive numbers) and UInt32, which
      is an unsigned integer (only positive numbers)

      Comment

      • Mark Rae [MVP]

        #4
        Re: handling dbnulls

        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:1186582283 .887081.236250@ b79g2000hse.goo glegroups.com.. .
        It's not a terribly nice way of doing it. I'd use
        >
        if (!(DataReader["ParentId"] is DBNull))
        {
        ...
        }
        Is there any appreciable difference between the above and:

        if (DataReader["ParentId"] != DBNull.Value)
        {
        ....
        }


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: handling dbnulls

          On Aug 8, 3:25 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
          "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote in messagenews:118 6582283.887081. 236250@b79g2000 hse.googlegroup s.com...
          >
          It's not a terribly nice way of doing it. I'd use
          >
          if (!(DataReader["ParentId"] is DBNull))
          {
          ...
          }
          >
          Is there any appreciable difference between the above and:
          >
          if (DataReader["ParentId"] != DBNull.Value)
          {
          ...
          >
          }
          Well, when testing in a "positive" way I find

          if (DataReader["ParentId"] is DBNull)

          more readable than

          if (DataReader["ParentId"] == DBNull.Value)

          and I was just trying to be consistent with that :)

          Jon

          Comment

          • =?Utf-8?B?cm9kY2hhcg==?=

            #6
            RE: handling dbnulls

            Thanks everyone for this wealth of knowledge. As always, I appreciate it.
            Rod.

            "rodchar" wrote:
            hey all,
            >
            if (DataReader["ParentId"].ToString() != String.Empty)
            {
            oCategory.Paren tId = Convert.ToInt32 (DataReader["ParentId"]);
            }
            >
            ParentId has some null values coming from my database. Is this the correct
            way to handle the nulls for this field (or any field for that matter)?
            >
            When i debug, inside the immediate window i type:
            ?DataReader["ParentId"]
            { }
            >
            I'm not sure what the 2 braces mean so i added the ToString() and just
            checked for " ".
            >
            One more thing, about the Convert.ToInt32 is this ok to use? Why isn't there
            just a Convert.ToInteg er?
            >
            thanks,
            rodchar

            Comment

            • Mark Rae [MVP]

              #7
              Re: handling dbnulls

              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:1186585323 .292596.17430@b 79g2000hse.goog legroups.com...
              Well, when testing in a "positive" way I find
              >
              if (DataReader["ParentId"] is DBNull)
              >
              more readable than
              >
              if (DataReader["ParentId"] == DBNull.Value)
              >
              and I was just trying to be consistent with that :)
              Fair enough.

              BTW, this wasn't a criticism - I was just curious if "is" was more efficient
              than "=="...


              --
              Mark Rae
              ASP.NET MVP


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: handling dbnulls

                Mark Rae [MVP] <mark@markNOSPA Mrae.netwrote:
                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:1186585323 .292596.17430@b 79g2000hse.goog legroups.com...
                >
                Well, when testing in a "positive" way I find

                if (DataReader["ParentId"] is DBNull)

                more readable than

                if (DataReader["ParentId"] == DBNull.Value)

                and I was just trying to be consistent with that :)
                >
                Fair enough.
                >
                BTW, this wasn't a criticism - I was just curious if "is" was more efficient
                than "=="...
                Sure - I wasn't taking it as a criticism.

                I've no idea which is more performant to be honest - but I doubt that
                it'll be the bottleneck in most code anyway, so I'd go with whatever's
                most readable :)

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Ignacio Machin \( .NET/ C# MVP \)

                  #9
                  Re: handling dbnulls

                  Hi,

                  "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  news:1186585323 .292596.17430@b 79g2000hse.goog legroups.com...
                  On Aug 8, 3:25 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                  >"Jon Skeet [C# MVP]" <sk...@pobox.co mwrote in
                  >messagenews:11 86582283.887081 .236250@b79g200 0hse.googlegrou ps.com...
                  >>
                  It's not a terribly nice way of doing it. I'd use
                  >>
                  if (!(DataReader["ParentId"] is DBNull))
                  {
                  ...
                  }
                  >>
                  >Is there any appreciable difference between the above and:
                  >>
                  >if (DataReader["ParentId"] != DBNull.Value)
                  >{
                  >...
                  >>
                  >}
                  >
                  Well, when testing in a "positive" way I find
                  >
                  if (DataReader["ParentId"] is DBNull)
                  >
                  more readable than
                  >
                  if (DataReader["ParentId"] == DBNull.Value)
                  >
                  and I was just trying to be consistent with that :)
                  I like the latter more :)

                  It should be that I live in the tropics and you live in England :)


                  Comment

                  Working...