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. General Programming
  3. .NET (Core and Framework)
  4. database library

database library

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelpquestion
7 Posts 5 Posters 0 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.
  • J Offline
    J Offline
    joindotnet
    wrote on last edited by
    #1

    Hi, I want to create a library for all the sql database operations.All other functions are working, but function for returning sqldatareader doesn't work.When I call this function from my page and try to access returned datareader object >it gives the error reader is closed. What to do???

    G 1 Reply Last reply
    0
    • J joindotnet

      Hi, I want to create a library for all the sql database operations.All other functions are working, but function for returning sqldatareader doesn't work.When I call this function from my page and try to access returned datareader object >it gives the error reader is closed. What to do???

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      No one can help you until you show us the code for returning sqldatareader. However, from the error code I suspect that the connection used by the reader is closed.

      Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

      J 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        No one can help you until you show us the code for returning sqldatareader. However, from the error code I suspect that the connection used by the reader is closed.

        Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

        J Offline
        J Offline
        joindotnet
        wrote on last edited by
        #3

        This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.

        D G P 3 Replies Last reply
        0
        • J joindotnet

          This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Your problem is that you closed the Reader in the Finally block. Whenever execution leaves the Try block FOR ANY REASON, the code in the Finally block is executed, where you close the Reader. Remove the code from the Try block and run it again. Your Try block is catching any and all errors and supressing them since you don't have a Catch block reporting the error.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          1 Reply Last reply
          0
          • J joindotnet

            This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #5

            As you you are using try finally block, the connection is closed so it is impossible to read from the reader as SqlDataReader requires open connection. So just return the reader without closing the connection.

            Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

            1 Reply Last reply
            0
            • J joindotnet

              This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              As the others have commented, you need to remove the close on the connection to return it. You should also ensure that the DataReader closes the connection for you. One method of doing this is to pass in CommandBehavior.CloseConnection as a parameter to ExecuteReader.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles | MoXAML PowerToys

              J 1 Reply Last reply
              0
              • P Pete OHanlon

                As the others have commented, you need to remove the close on the connection to return it. You should also ensure that the DataReader closes the connection for you. One method of doing this is to pass in CommandBehavior.CloseConnection as a parameter to ExecuteReader.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles | MoXAML PowerToys

                J Offline
                J Offline
                Jon_Boy
                wrote on last edited by
                #7

                Hey Pete, Thanks for your post. Never realized that the CommandBehavior.CloseConnection enum existed. Very nice tip.

                Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

                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