I have been programming in VB6 for quite a while but I am new to VB.Net and having a heck of a time transitioning to ADO.NET. Can someone tell me what is wrong with the code below? I took this out of a book but I can't get it to work:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'--CONNECTION
Dim oConnection As New SqlClient.SqlConnection("Data Source = DBSERVER1; Initial Catalog=Test_Cafeteria")
'--COMMAND
Dim oCommand As New SqlClient.SqlCommand("Select * FROM [Charge_accounts_T]", oConnection)
'-- DATARADER
Dim oDataReader As SqlClient.SqlDataReader = oCommand.ExecuteReader
'-- ITERATE THROUGH RESULT
While oDataReader.Read()
Debug.WriteLine(oDataReader.GetSqlValue(0))
End While
End Sub
Here is the error I am getting >
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: ExecuteReader requires an open and available Connection. The connection's current state is Closed.
Thanks in advance.
Shannan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'--CONNECTION
Dim oConnection As New SqlClient.SqlConnection("Data Source = DBSERVER1; Initial Catalog=Test_Cafeteria")
'--COMMAND
Dim oCommand As New SqlClient.SqlCommand("Select * FROM [Charge_accounts_T]", oConnection)
'-- DATARADER
Dim oDataReader As SqlClient.SqlDataReader = oCommand.ExecuteReader
'-- ITERATE THROUGH RESULT
While oDataReader.Read()
Debug.WriteLine(oDataReader.GetSqlValue(0))
End While
End Sub
Here is the error I am getting >
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: ExecuteReader requires an open and available Connection. The connection's current state is Closed.
Thanks in advance.
Shannan