casting bit to boolean

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

    #1

    casting bit to boolean

    Hey

    ..net 2.0

    in my c# code I retrieve a value from a column in database (sql server 2000)
    (using SqlReader) this database column has the datatype Bit!

    Now I'm looking a way to cast this bit value into an .Net boolean variable

    Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
    exception - specified cast is not valid!

    So I was thinking maybe this could do it:
    Boolean test = false;
    int value = (int) _SqlReader["modified"];
    if (value == 1)
    test = true

    I wonder if there is a much better of doing this cast?

    any suggestions?


  • Chris Shepherd

    #2
    Re: casting bit to boolean

    Jeff wrote:
    So I was thinking maybe this could do it:
    Boolean test = false;
    int value = (int) _SqlReader["modified"];
    if (value == 1)
    test = true
    >
    I wonder if there is a much better of doing this cast?
    >
    any suggestions?
    Will Convert.ToBoole an((int) _SqlReader["modified"]) work?


    Chris.

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: casting bit to boolean

      Jeff,

      Something tells me that "modified" might not actually be a bit data
      type. If it was, then the provider (the classes in the
      System.Data.Sql Client namespace) should have converted it for you.

      If you put _SqlReader["modified"] in the debugger, what is the type that
      it says it returns?


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Jeff" <donot@spam.mew rote in message
      news:eWEg620NIH A.5224@TK2MSFTN GP02.phx.gbl...
      Hey
      >
      .net 2.0
      >
      in my c# code I retrieve a value from a column in database (sql server
      2000) (using SqlReader) this database column has the datatype Bit!
      >
      Now I'm looking a way to cast this bit value into an .Net boolean variable
      >
      Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
      exception - specified cast is not valid!
      >
      So I was thinking maybe this could do it:
      Boolean test = false;
      int value = (int) _SqlReader["modified"];
      if (value == 1)
      test = true
      >
      I wonder if there is a much better of doing this cast?
      >
      any suggestions?
      >
      >

      Comment

      • Michael Rubinstein

        #4
        Re: casting bit to boolean

        Jeff, if you mean SqlDataReader as in
        System.Data.Sql Client.SqlDataR eader
        you could call

        if (_SqlReader.Get Boolean(_SqlRea der.GetOrdinal( "modified") ))
        {
        }

        Michael

        "Jeff" <donot@spam.mew rote in message
        news:eWEg620NIH A.5224@TK2MSFTN GP02.phx.gbl...
        Hey
        >
        .net 2.0
        >
        in my c# code I retrieve a value from a column in database (sql server
        2000) (using SqlReader) this database column has the datatype Bit!
        >
        Now I'm looking a way to cast this bit value into an .Net boolean variable
        >
        Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
        exception - specified cast is not valid!
        >
        So I was thinking maybe this could do it:
        Boolean test = false;
        int value = (int) _SqlReader["modified"];
        if (value == 1)
        test = true
        >
        I wonder if there is a much better of doing this cast?
        >
        any suggestions?
        >
        >

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: casting bit to boolean


          "Jeff" <donot@spam.mew rote in message
          news:eWEg620NIH A.5224@TK2MSFTN GP02.phx.gbl...
          Hey
          >
          .net 2.0
          >
          in my c# code I retrieve a value from a column in database (sql server
          2000) (using SqlReader) this database column has the datatype Bit!
          >
          Now I'm looking a way to cast this bit value into an .Net boolean variable
          >
          Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
          exception - specified cast is not valid!
          >
          So I was thinking maybe this could do it:
          Boolean test = false;
          int value = (int) _SqlReader["modified"];
          if (value == 1)
          test = true
          >
          I wonder if there is a much better of doing this cast?
          How about:

          bool test = (_SqlReader["modified"] == 1);
          >
          any suggestions?
          >
          >

          Comment

          • Wingot

            #6
            Re: casting bit to boolean


            -----Original Message-----
            From: Ben Voigt [C++ MVP] [mailto:rbv@nosp am.nospam]
            Posted At: Thursday, 6 December 2007 7:01 AM
            Posted To: microsoft.publi c.dotnet.langua ges.csharp
            Conversation: casting bit to boolean
            Subject: Re: casting bit to boolean
            >
            >
            "Jeff" <donot@spam.mew rote in message
            news:eWEg620NIH A.5224@TK2MSFTN GP02.phx.gbl...
            Hey

            .net 2.0

            in my c# code I retrieve a value from a column in database (sql
            server
            2000) (using SqlReader) this database column has the datatype Bit!

            Now I'm looking a way to cast this bit value into an .Net boolean
            variable

            Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
            exception - specified cast is not valid!

            So I was thinking maybe this could do it:
            Boolean test = false;
            int value = (int) _SqlReader["modified"];
            if (value == 1)
            test = true

            I wonder if there is a much better of doing this cast?
            >
            How about:
            >
            bool test = (_SqlReader["modified"] == 1);
            >

            any suggestions?
            I was going to suggest the same, but it was going to be a
            if..then..else. Good job finding the one liner that did it.

            Comment

            Working...