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

  • Thread starter renu renu via .NET 247
  • Start date
R

renu renu via .NET 247

(Type your message here)

--------------------------------
From: renu


Friends,

when I am trying to create a login page and connecting to the sqlserver database I am getting the following error

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





and mycode is



Dim strconn As String
Dim strsql As String
Dim command As SqlCommand
Dim i As Integer
Try
strconn = "server =(local);trusted_connection=yes;database=consultants;"

Response.Write("connection ok")

Dim conn As New SqlConnection(strconn)



strsql = "select count(*) from consul_login where uname='" & txtUname.Text & "'and pword='" & txtPassword.Text & "'"

command = New SqlCommand(strsql, conn)


conn.Open()
Catch ex As Exception
Response.Write(ex.Message)
End Try
i = command.ExecuteScalar()
If i <> 1 Then
Response.Redirect("e:\dotnet\authenticationdenied.aspx")

Else
Response.Redirect("e:\dotnet\timesheet.aspx")
Response.Redirect("timesheet?n1" & txtUname.Text)


End If

can anybody help me out?

thanks
renu
 
G

Guest

Move i = command.ExecuteScalar() into the try (just above the catch). Most
likely, you are failing on conn.Open(). Perhaps you are running under the
anonymous user and it is a trusted connection?

Set a breakpoint on the Catch line and see if you are blowing up.

BTW, your ASP.NET code looks very much like ASP, which is not the proper
methodology. Rather than response write errors, you are better to output them
through Trace and redirect users to a more friendly error (either by setting
an error page or output a friendly message). Rather than response.write, you
can set up a label control to output the friendly message.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top