Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Who closed my datareader?

Who closed my datareader?

Scheduled Pinned Locked Moved Clever Code
helpcomtoolsquestion
12 Posts 10 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Giorgi Dalakishvili

    Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:

    using ( drUser = cmdUser.ExecuteReader());
    {
    drUser.Read();
    myUser.User_Code = Convert.ToInt32(drUser[0]);
    myUser.User_Name = drUser[1].ToString();
    myUser.Password = drUser[2].ToString();
    myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
    }
    drUser.Close();
    cnnUser.Close();

    It generates an error at Read()

    Giorgi Dalakishvili #region signature my articles #endregion

    R Offline
    R Offline
    Robert Royall
    wrote on last edited by
    #3

    The rogue semicolon strikes again! Doesn't C# throw a warning for empty block statements?

    Please don't bother me... I'm hacking right now. Don't look at me like that - doesn't anybody remember what "hacking" really means? :sigh:

    P 1 Reply Last reply
    0
    • R Robert Royall

      The rogue semicolon strikes again! Doesn't C# throw a warning for empty block statements?

      Please don't bother me... I'm hacking right now. Don't look at me like that - doesn't anybody remember what "hacking" really means? :sigh:

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #4

      Maybe it should give a warning on ;{

      M 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:

        using ( drUser = cmdUser.ExecuteReader());
        {
        drUser.Read();
        myUser.User_Code = Convert.ToInt32(drUser[0]);
        myUser.User_Name = drUser[1].ToString();
        myUser.Password = drUser[2].ToString();
        myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
        }
        drUser.Close();
        cnnUser.Close();

        It generates an error at Read()

        Giorgi Dalakishvili #region signature my articles #endregion

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #5

        Giorgi Dalakishvili wrote:

        It generates an error at Read()

        And at drUser.Close() ;P (well it should, if the original bug gets worked around)

        xacc.ide - now with IronScheme support
        IronScheme - 1.0 alpha 2 out now

        V 1 Reply Last reply
        0
        • L leppie

          Giorgi Dalakishvili wrote:

          It generates an error at Read()

          And at drUser.Close() ;P (well it should, if the original bug gets worked around)

          xacc.ide - now with IronScheme support
          IronScheme - 1.0 alpha 2 out now

          V Offline
          V Offline
          Vasudevan Deepak Kumar
          wrote on last edited by
          #6

          In this case, it would throw an exception since the reader object is already closed. Wouldn't it?

          Vasudevan Deepak Kumar Personal Homepage
          Tech Gossips
          A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

          L T 2 Replies Last reply
          0
          • V Vasudevan Deepak Kumar

            In this case, it would throw an exception since the reader object is already closed. Wouldn't it?

            Vasudevan Deepak Kumar Personal Homepage
            Tech Gossips
            A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #7

            ObjectDisposedException

            xacc.ide - now with IronScheme support
            IronScheme - 1.0 alpha 2 out now

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Maybe it should give a warning on ;{

              M Offline
              M Offline
              Mirko1980
              wrote on last edited by
              #8

              Well, it gives it, there is a "Possible mistaken empty statement" warning that is shown in these cases.

              1 Reply Last reply
              0
              • G Giorgi Dalakishvili

                Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:

                using ( drUser = cmdUser.ExecuteReader());
                {
                drUser.Read();
                myUser.User_Code = Convert.ToInt32(drUser[0]);
                myUser.User_Name = drUser[1].ToString();
                myUser.Password = drUser[2].ToString();
                myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
                }
                drUser.Close();
                cnnUser.Close();

                It generates an error at Read()

                Giorgi Dalakishvili #region signature my articles #endregion

                D Offline
                D Offline
                Derek Bartram
                wrote on last edited by
                #9

                Well don't look at me, I didn't do it.... I've been too busy working on this -> http://www.codeproject.com/KB/WPF/ribboncontrol.aspx[^]

                1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:

                  using ( drUser = cmdUser.ExecuteReader());
                  {
                  drUser.Read();
                  myUser.User_Code = Convert.ToInt32(drUser[0]);
                  myUser.User_Name = drUser[1].ToString();
                  myUser.Password = drUser[2].ToString();
                  myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
                  }
                  drUser.Close();
                  cnnUser.Close();

                  It generates an error at Read()

                  Giorgi Dalakishvili #region signature my articles #endregion

                  P Offline
                  P Offline
                  peterchen
                  wrote on last edited by
                  #10

                  ouch!

                  We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                  blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

                  1 Reply Last reply
                  0
                  • G Giorgi Dalakishvili

                    Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:

                    using ( drUser = cmdUser.ExecuteReader());
                    {
                    drUser.Read();
                    myUser.User_Code = Convert.ToInt32(drUser[0]);
                    myUser.User_Name = drUser[1].ToString();
                    myUser.Password = drUser[2].ToString();
                    myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
                    }
                    drUser.Close();
                    cnnUser.Close();

                    It generates an error at Read()

                    Giorgi Dalakishvili #region signature my articles #endregion

                    B Offline
                    B Offline
                    Brady Kelly
                    wrote on last edited by
                    #11

                    The Real WTF (TM) is that it's closing the drUser outside the supposed using block.

                    1 Reply Last reply
                    0
                    • V Vasudevan Deepak Kumar

                      In this case, it would throw an exception since the reader object is already closed. Wouldn't it?

                      Vasudevan Deepak Kumar Personal Homepage
                      Tech Gossips
                      A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

                      T Offline
                      T Offline
                      The Nightcoder
                      wrote on last edited by
                      #12

                      Not necessarily - Close() is often an alias for Dispose() and that never throws exceptions when called more times than necessary (as per the disposable pattern recommendations - Dispose() should under no circumstances whatsoever be dangerous to call - regardless of state). Any other method should throw an ObjectDisposedException, however. I may be wrong about DataReaders, but it's definitely so (Close and Dispose are the same - or actually, one calls the other) for Streams (as per the documentation).

                      -- Peter

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups