• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5774
  • Last Modified:

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

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
HolyFamilyFamily
Asked:
HolyFamilyFamily
1 Solution
 
rdcproCommented:
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
 
HolyFamilyFamilyAuthor Commented:
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
 
OMC2000Commented:
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
 
YZlatCommented:
I don't see connection.Open() statement anywhere
0
 
OMC2000Commented:
???, ?? ??-???????? ?????? ????????? ?? ??? ??? ????????
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now