ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Why always the system returns that message?
I have a sub:
Private Sub Initialization()
If myCEConnection.State = ConnectionState.Closed Then '--> *1
Try
myCEConnection.Open()
Catch ex As Exception
End Try
End If
Dim reader As SqlCeDataReader
Dim myCommand As SqlCeCommand = myCEConnection.CreateCommand()
myCommand.CommandText = "Command goes here"
Try
reader = myCommand.ExecuteReader()
Catch ex As Exception '--> *2
End Try
myCEConnection.Close()
End Sub
And I called that function from this sub:
Public myCEConnection As New SqlCeConnection("Connection String Goes Here")
Private Sub Report()
If myCEConnection.State = ConnectionState.Closed Then '--> *1
Try
myCEConnection.Open()
Catch ex As Exception
End Try
End If
Initialization()
myCEConnection.Close()
End Sub
When I try to set a breakpoint to the *1
line, the system returns myCEConnection.State = Open {1}
. But when I continue to run the breakpoint, on line *2
I got: ExecuteReader requires an open and available Connection. The connection's current state is Closed.
What's wrong with the code?
Note: Every sub/function on the class always have the If myCeConnection.State = .....
part to check the connection is already open or not. Maybe this caused the problem but I'm not quite sure.