Null check on executeScalar

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

    Null check on executeScalar

    What's the best way to check for null on an ExecuteScalar? The following
    would fire the command twice:

    if (cmd.ExecuteSca lar() != null)
    {
    intContactID = Convert.ToInt32 (cmd.ExecuteSca lar());
    }


  • Marc Gravell

    #2
    Re: Null check on executeScalar

    Based purely on the C#:

    object value = cmd.ExecuteScal ar();
    if(value!=null) intContactID = Convert.ToInt32 (value);

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Null check on executeScalar

      Oops; probably need to move the "int" declaration to a separate
      location, or add a brace ;-p

      Marc

      Comment

      • Mark Rae

        #4
        Re: Null check on executeScalar

        "Earl" <brikshoe@newsg roups.nospamwro te in message
        news:OjLYkjb7GH A.2384@TK2MSFTN GP04.phx.gbl...
        What's the best way to check for null on an ExecuteScalar? The following
        would fire the command twice:
        object objValue = cmd.ExecuteScal er();
        if (objValue != DbNull.Value)
        {
        intContactID = Convert.ToInt32 (objValue);
        }


        Comment

        • Earl

          #5
          Re: Null check on executeScalar

          Thanks Mark and Marc.

          "Mark Rae" <mark@markNOSPA Mrae.comwrote in message
          news:O38WIed7GH A.3452@TK2MSFTN GP05.phx.gbl...
          "Earl" <brikshoe@newsg roups.nospamwro te in message
          news:OjLYkjb7GH A.2384@TK2MSFTN GP04.phx.gbl...
          >
          >What's the best way to check for null on an ExecuteScalar? The following
          >would fire the command twice:
          >
          object objValue = cmd.ExecuteScal er();
          if (objValue != DbNull.Value)
          {
          intContactID = Convert.ToInt32 (objValue);
          }
          >

          Comment

          • Earl

            #6
            Re: Null check on executeScalar

            In case someone else runs across this post, one note to add to your fix.
            Must use null instead of DbNull.Value there:

            ....
            if (objValue != null)
            ....

            "Mark Rae" <mark@markNOSPA Mrae.comwrote in message
            news:O38WIed7GH A.3452@TK2MSFTN GP05.phx.gbl...
            "Earl" <brikshoe@newsg roups.nospamwro te in message
            news:OjLYkjb7GH A.2384@TK2MSFTN GP04.phx.gbl...
            >
            >What's the best way to check for null on an ExecuteScalar? The following
            >would fire the command twice:
            >
            object objValue = cmd.ExecuteScal er();
            if (objValue != DbNull.Value)
            {
            intContactID = Convert.ToInt32 (objValue);
            }
            >

            Comment

            • Arne Vajhøj

              #7
              Re: Null check on executeScalar

              Earl wrote:
              "Mark Rae" <mark@markNOSPA Mrae.comwrote in message
              news:O38WIed7GH A.3452@TK2MSFTN GP05.phx.gbl...
              >"Earl" <brikshoe@newsg roups.nospamwro te in message
              >news:OjLYkjb7G HA.2384@TK2MSFT NGP04.phx.gbl.. .
              >>What's the best way to check for null on an ExecuteScalar? The following
              >>would fire the command twice:
              >object objValue = cmd.ExecuteScal er();
              >if (objValue != DbNull.Value)
              >{
              > intContactID = Convert.ToInt32 (objValue);
              >}
              In case someone else runs across this post, one note to add to your fix.
              Must use null instead of DbNull.Value there:
              >
              ...
              if (objValue != null)
              ...
              null mean no rows returned.

              DbNull.Value means (at least) one row with
              (at least) one column but the first row first
              column contained a database NULL.

              Two completely different scenarios.

              Arne

              Comment

              • Earl

                #8
                Re: Null check on executeScalar

                Hmmm, I can't check DbNull.Value of an object though. It has to be the value
                of the object -- which it cannot be until I cast it. I can check the object
                as to whether or not it is null. So my thinking is, well, ExecuteScalar only
                returns one row, one column anyway (a single value) or else it returns null.
                Is that the wrong way of looking at it?

                "Arne Vajhøj" <arne@vajhoej.d kwrote in message
                news:fyBXg.2146 6$2g4.15201@duk eread09...
                Earl wrote:
                >"Mark Rae" <mark@markNOSPA Mrae.comwrote in message
                >news:O38WIed7G HA.3452@TK2MSFT NGP05.phx.gbl.. .
                >>"Earl" <brikshoe@newsg roups.nospamwro te in message
                >>news:OjLYkjb7 GHA.2384@TK2MSF TNGP04.phx.gbl. ..
                >>>What's the best way to check for null on an ExecuteScalar? The
                >>>following would fire the command twice:
                >>object objValue = cmd.ExecuteScal er();
                >>if (objValue != DbNull.Value)
                >>{
                >> intContactID = Convert.ToInt32 (objValue);
                >>}
                >In case someone else runs across this post, one note to add to your fix.
                >Must use null instead of DbNull.Value there:
                >>
                >...
                >if (objValue != null)
                >...
                >
                null mean no rows returned.
                >
                DbNull.Value means (at least) one row with
                (at least) one column but the first row first
                column contained a database NULL.
                >
                Two completely different scenarios.
                >
                Arne

                Comment

                • Arne Vajhøj

                  #9
                  Re: Null check on executeScalar

                  Earl wrote:
                  Hmmm, I can't check DbNull.Value of an object though. It has to be the value
                  of the object -- which it cannot be until I cast it. I can check the object
                  as to whether or not it is null. So my thinking is, well, ExecuteScalar only
                  returns one row, one column anyway (a single value) or else it returns null.
                  Is that the wrong way of looking at it?
                  The following primitive examples works for me:

                  using System;
                  using System.Data.Ole Db;

                  public class MainClass
                  {
                  public static void Main(string[] args)
                  {
                  OleDbConnection con = new
                  OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
                  Source=C:\\Data bases\\MSAccess \\Test.mdb");
                  con.Open();
                  OleDbCommand cmd1 = new OleDbCommand("S ELECT NULL", con);
                  object v1 = cmd1.ExecuteSca lar();
                  if(v1 == DBNull.Value)
                  {
                  Console.WriteLi ne("NULL");
                  }
                  OleDbCommand cmd2 = new OleDbCommand("S ELECT * FROM t1 WHERE 1
                  2", con);
                  object v2 = cmd2.ExecuteSca lar();
                  if(v2 == null)
                  {
                  Console.WriteLi ne("null");
                  }
                  }
                  }

                  Arne

                  Comment

                  Working...