ExecuteReader requires an open and available Connection. The connection's current state is closed
Asked Answered
S

3

8

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 :)

Soldiery answered 8/5, 2009 at 9:30 Comment(0)
F
14

I think you have forgotten to open the connection.

Open it before this line:

cmd.Parameters.AddWithValue("@baskethash", baskethash)

Using -

db.Open()
Filberto answered 8/5, 2009 at 9:33 Comment(0)
W
2

You actually forgot to Open connection:

        db.Open()
        Using dr As SqlDataReader = cmd.ExecuteReader()
Warman answered 8/5, 2009 at 9:34 Comment(0)
M
1

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.

Magnetostriction answered 21/12, 2011 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.