Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
            connection_open()
            cmd = New SqlCommand(qry, cnn)
            dr = cmd.ExecuteReader

            If dr.Read = True Then
                MsgBox("Login Successfully", MsgBoxStyle.Information, "Login Success")
                Me.Hide()
                connection_close()
            Else
                MsgBox("Invalid User_Name/Password", MsgBoxStyle.Critical, "Login Failure")
                connection_close()

            End If




        End If
    End Sub
End Class


What I have tried:

ExecuteReader requires an open and available Connection. The connection's current state is closed.'
Posted
Updated 7-Jun-20 4:35am
v2
Comments
F-ES Sitecore 7-Jun-20 10:28am    
The error is self-explanatory. Rather than using global\shared connections, when you need a connection create it there and then using new SqlConnection. As it is the problem is probably a combination of your connection_open function and your "cnn" variable.
PIEBALDconsult 7-Jun-20 10:30am    
We have no idea what connection_open() does, and maybe you don't either.
Try cmd.Connection.open()

1 solution

We can't see your connection_open method, but it's not opening the connection you are using, and you shouldn't do it like that anyway. Create your connection inside a Using block, open it, and let the system Close and Dispose it when it goes out of scope. "Sharing" a connection is generally a bad idea and leads to problems later.
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using cmd As New SqlCommand("SELECT iD, description FROM myTable", con)
		Using reader As SqlDataReader = cmd.ExecuteReader()
			While reader.Read()
				Dim id__1 As Integer = CInt(reader("iD"))
				Dim desc As String = DirectCast(reader("description"), String)
				Console.WriteLine("ID: {0}" & vbLf & "    {1}", iD, desc)
			End While
		End Using
	End Using
End Using
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900