Ok, I asked about this very error earlier this week and had some very helpful answers and without doubt things have drastically improved since I started following the suggestions.
However, now I am using the 'correct', best practice method to access the database I still get this error on some functions and I cannot get it to disappear for that block. Here is my code:
Public Shared Function doesBasketExist(ByVal baskethash As String) As Boolean
Dim _r As Boolean
Using db As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("pitstopConnectionString").ConnectionString)
Using cmd As New SqlCommand("doGetBasketByHash", db)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@baskethash", baskethash)
Using dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows() = True Then
_r = True
Else
_r = False
End If
dr.Close()
End Using
End Using
End Using
Return _r
End Function
Now no matter what I do I get: ExecuteReader requires an open and available Connection. The connection's current state is closed. on this connection. I do have functions with objects called the same thing within this class (cmd, dr etc.) but Using closes up after itself doesn't it?
Suggestions welcome :)
I think you have forgotten to open the connection.
Open it before this line:
cmd.Parameters.AddWithValue("@baskethash", baskethash)
Using -
db.Open()
You actually forgot to Open
connection:
db.Open()
Using dr As SqlDataReader = cmd.ExecuteReader()
One reason for this would be that your connection could not be opening at all. What ever exception that is coming at the "SqlConnection.Open" statement is being suppressed. If the problem is not in your application it could be that server is unable to grant you a connection. Could be because of a connection leak in your app or in some other database hosted on the same server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With