Microsoft Knowledge Base Email Alertz

KBAlertz.com: If you run commands or call methods of the

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 311544 - Last Review: May 13, 2007 - Revision: 1.2

Error message occurs when you run commands on a command object: "Unhandled exception of type 'System.InvalidOperationException'"

This article was previously published under Q311544
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Data
  • System.Data.OleDb
  • System.Data.SqlClient

On This Page

SYMPTOMS

If you run commands or call methods of the SqlCommand or OleDbCommand object, you receive the following error message if a connection is not open:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: ExecuteReader requires an open and available Connection (state=Closed).

MORE INFORMATION

The DataAdapter object does not require that you explicitly open a connection to run some of its methods. Therefore, you can call the Update or Fill method of the DataAdapter object when the connection is closed. The Connection object that is associated with the SELECT statement must be valid, but it does not need to be open. If you close the connection before you call Fill, the connection is opened to retrieve the data and then closed. If the connection is open before you call Fill, it remains open.

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  3. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
  4. Place two Button controls and one DataGrid control on Form1. Button1, Button2, and DataGrid1 are created by default.
  5. Change the Name property of Button1 to btnDataAdapter and the Text property to DataAdapter.

    Change the Name property of Button2 to btnCommand and the Text property to Command.
  6. Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the "General Declarations" section of Form1:
    Imports System
    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    					
  7. In the Code window, copy and paste the following code after the "Windows Form Designer generated code" region:
    Private Sub btnDataAdapter(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnDataAdapter.Click
        Dim myConnString As String = _
            "User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=myServer"
        Dim mySelectQuery As String = _
            "Select * From Customers Where CustomerID Like 'A%'"
        Dim con As New SqlConnection(myConnString)
        'The code works fine even if you comment out the next line (to open the connection).
        con.Open()
        Dim daCust As New SqlDataAdapter(mySelectQuery, con)
        Dim ds As New DataSet()
        daCust.Fill(ds, "Cust")
        DataGrid1.DataSource = ds
        DataGrid1.DataMember = "Cust"
    End Sub
    
    Private Sub btnCommand(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCommand.Click
        Dim myConnString As String = _
            "User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=myServer"
        Dim mySelectQuery As String = _
            "Select * From Customers Where CustomerID Like 'A%'"
        Dim con As New SqlConnection(myConnString)
        Dim myCommand As New SqlCommand(mySelectQuery, con)
        'An exception is thrown if you comment out the next line (to open the connection).
        con.Open()
        Dim myReader As SqlDataReader = myCommand.ExecuteReader()
        While myReader.Read()
            'Process data.
        End While
        myReader.Close()
        con.Close()
    End Sub
    					
  8. Modify the connection string (myConnString) as appropriate for your environment.
  9. Save your project. On the Debug menu, click Start to run your project.
  10. Comment out the line of code that opens the connection. Notice that DataAdapter.Fill works as expected, but Command.ExecuteReader fails with the above-mentioned exception.

REFERENCES

For more information about ADO.NET objects and syntax, refer to the following topic in the Microsoft .NET Framework Software Development Kit (SDK) documentation or MSDN Online:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbsqlclient kbsystemdata kbprb KB311544
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please