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
HolyFamilyFamilyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YZlatCommented:
I don't see connection.Open() statement anywhere
0
OMC2000Commented:
???, ?? ??-???????? ?????? ????????? ?? ??? ??? ????????
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.