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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. SQLClient has cross object code?

SQLClient has cross object code?

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 Posts 3 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
    Jonathan van de Veen
    wrote on last edited by
    #1

    I've build the following, of wich I know it's not the way it's supposed to be and I also know how to fix it. I'm just wondering how it's caused. - I open a SQLConnection - I create a SQLCommand - I run SQLCommand.ExecuteReader and store the readerobject in a variable - I set the SQLCommand to null - I create a new SQLCommand in the same SQLCommand variable - I run ExecuteNonQuery Doing the above will result in an InvalidOperationException with the message: "There is already an open DataReader associated with this Command which must be closed first." I looked to see if the setting to null and the creating a new SQLCommand actually resulted in a new object and it did. Just for my understanding, how does this work in the background?

    C 1 Reply Last reply
    0
    • J Jonathan van de Veen

      I've build the following, of wich I know it's not the way it's supposed to be and I also know how to fix it. I'm just wondering how it's caused. - I open a SQLConnection - I create a SQLCommand - I run SQLCommand.ExecuteReader and store the readerobject in a variable - I set the SQLCommand to null - I create a new SQLCommand in the same SQLCommand variable - I run ExecuteNonQuery Doing the above will result in an InvalidOperationException with the message: "There is already an open DataReader associated with this Command which must be closed first." I looked to see if the setting to null and the creating a new SQLCommand actually resulted in a new object and it did. Just for my understanding, how does this work in the background?

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Jonathan van de Veen wrote:

      Doing the above will result in an InvalidOperationException with the message: "There is already an open DataReader associated with this Command which must be closed first."

      Command? Connection surely!


      Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      J 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Jonathan van de Veen wrote:

        Doing the above will result in an InvalidOperationException with the message: "There is already an open DataReader associated with this Command which must be closed first."

        Command? Connection surely!


        Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        J Offline
        J Offline
        Jonathan van de Veen
        wrote on last edited by
        #3

        I'm affraid not. I've copied the message straight from VS.

        N 1 Reply Last reply
        0
        • J Jonathan van de Veen

          I'm affraid not. I've copied the message straight from VS.

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello You should call the SqlDataReader.Close() first, or make a new command object.

          Regards:rose:

          J 1 Reply Last reply
          0
          • N Nader Elshehabi

            Hello You should call the SqlDataReader.Close() first, or make a new command object.

            Regards:rose:

            J Offline
            J Offline
            Jonathan van de Veen
            wrote on last edited by
            #5

            I was thinking the same thing, but... I can't close the datareader as I still need it. I tried making a new SqlCommand in a different variable, but this didn't solve the problem. I now have two connections wich I don't realy want.

            N 1 Reply Last reply
            0
            • J Jonathan van de Veen

              I was thinking the same thing, but... I can't close the datareader as I still need it. I tried making a new SqlCommand in a different variable, but this didn't solve the problem. I now have two connections wich I don't realy want.

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              Hello

              Jonathan van de Veen wrote:

              I now have two connections wich I don't realy want.

              Sorry! It's not a perfect world!:-D

              Regards:rose:

              J 1 Reply Last reply
              0
              • N Nader Elshehabi

                Hello

                Jonathan van de Veen wrote:

                I now have two connections wich I don't realy want.

                Sorry! It's not a perfect world!:-D

                Regards:rose:

                J Offline
                J Offline
                Jonathan van de Veen
                wrote on last edited by
                #7

                Oh, I guess I took a wrong turn near Mars somewhere. :doh:

                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