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
- Start Microsoft Visual Studio .NET.
- Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
- Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
- Place two Button controls and one DataGrid control on Form1. Button1, Button2, and DataGrid1 are created by default.
- 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. - 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
- 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
- Modify the connection string (myConnString) as appropriate for your environment.
- Save your project. On the Debug menu, click Start to run your project.
- 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:
APPLIES TO
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft Visual Basic .NET 2002 Standard Edition
| 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