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



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET (2002)

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

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:

Modification Type:MinorLast Reviewed:3/9/2006
Keywords:kbprb kbSqlClient kbSystemData KB311544