Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 335
dooburt Avatar asked May 08 '09 09:05

dooburt


3 Answers

I think you have forgotten to open the connection.

Open it before this line:

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

Using -

db.Open()
like image 137
Kirtan Avatar answered Oct 22 '22 03:10

Kirtan


You actually forgot to Open connection:

        db.Open()
        Using dr As SqlDataReader = cmd.ExecuteReader()
like image 31
Anton Gogolev Avatar answered Oct 22 '22 03:10

Anton Gogolev


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.

like image 1
Syed Salman Akbar Avatar answered Oct 22 '22 05:10

Syed Salman Akbar