Solved

ExecuteReader requires an open and available Connection. The connection's current state is Closed.

Posted on 2004-11-01
5,663 Views
Last Modified: 2008-01-09
Hello,
I'm trying to pull data from a SQL Server database, but I'm getting this error:

System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed.

Here's the code:
**********************
      Dim CreditCard as SecureCard
      Dim connection as SqlConnection = New SqlConnection("Database=HolyFamily;User=*website;Password=*")
        Dim command As SqlCommand = New SqlCommand("GetCustomerByOrderID", Connection)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.Add("@OrderID", OrderID)
        Dim reader As SqlDataReader = command.ExecuteReader()

      ' get credit card details
          Try
            CreditCard = New SecureCard(reader("CreditCard"))
          Catch
            Throw New OrderProcessorException("Unable to retrieve credit card details.", 100)
          End Try

      lblCreditCard.Text = CreditCard.CardNumber.ToString
***********************************

I'm having to use the SecureCard class to decrypt the credit card information - this should work. Any help is appreciated.

Thanks,
Jay
0
Question by:HolyFamilyFamily
    5 Comments
     
    LVL 26

    Expert Comment

    by:rdcpro
    Well, I'd say your connection is failing.  Here's an example of a connection string I use:

    "Provider=SQLOLEDB;Server=TestSQL04;database=forms;Trusted_Connection=yes;"

    Now, I usually set the credentials by having the application impersonate the domain service account I want to use to connect to the database, and set the credentials from encrypted values in the Registry...  But although you use an explicit username and password, the rest should be the same.  In your connection string I don't see where you specify the SQL Server...

    Regards,
    Mike Sharp
    0
     

    Author Comment

    by:HolyFamilyFamily
    Mike,
    It's working for all of my other connections. Is there some reason it wouldn't work here? I do have a functioning ecommerce site using this connection string (note that I did change the login and password before posting it here), so it should work for everything. Right?

    Thanks,
    Jay
    0
     
    LVL 15

    Accepted Solution

    by:
    These lines should work:

        Dim CreditCard as SecureCard
         Dim connection as SqlConnection = New SqlConnection("Database=HolyFamily;User=*website;Password=*")
            Dim command As SqlCommand = New SqlCommand("GetCustomerByOrderID", Connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@OrderID", OrderID)
            connection.Open(); // for data Reader we should open conneciton explicitly
            Dim reader As SqlDataReader = command.ExecuteReader()



    0
     
    LVL 35

    Expert Comment

    by:YZlat
    I don't see connection.Open() statement anywhere
    0
     
    LVL 15

    Expert Comment

    by:OMC2000
    ???, ?? ??-???????? ?????? ????????? ?? ??? ??? ????????
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now