Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-11-01
5
Medium Priority
?
5,736 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
Comment
Question by:HolyFamilyFamily
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 12470002
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
ID: 12470024
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:
OMC2000 earned 2000 total points
ID: 12471765
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
ID: 12472608
I don't see connection.Open() statement anywhere
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 12477044
???, ?? ??-???????? ?????? ????????? ?? ??? ??? ????????
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

636 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