Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim connection As SqlConnection = New SqlConnection("server=(local); Database = Bookstore; integrated Security=True")
Dim CSP As SqlCommand = New SqlCommand()
'Dim Cust As SqlCommand = New SqlCommand()

CSP.Connection = connection
CSP.CommandText = ("select CusID,CusPassword from CusromerDetail where CusUsername = " & "'" & txtUserName.Text & "' and CusPassword = '" & txtUserPass.Text & "'")

'Cust.Connection = connection
'Cust.CommandText = ("select CustEmail,CustPassword from Customer where CustEmail = " & "'" & txtUserName.Text & "' and CustPassword = '" & txtUserPass.Text & "'")

connection.Open()

If txtUserName.Text.StartsWith("cs") Then
Dim csr As SqlDataReader = CSP.ExecuteReader
If csr.Read = True Then
Session.Contents("username") = txtUserName.Text
Response.Redirect("AboutUs.aspx")
Else
Response.Redirect("Login.aspx")
End If
End If
connection.Close()
End Sub

Dani AI

Generated

Good that changing the server name solved the immediate connection issue — as hinted, the instance name or machine name is often the culprit. A few focused fixes will make the code robust, safer, and avoid intermittent failures (for example when a Response.Redirect happens before the connection is closed).

Use parameterized commands and automatic disposal so connections and readers always close, even on error or redirect. Example pattern (VB.NET):

Using cn As New SqlConnection("Server=.\SQLEXPRESS;Database=Bookstore;Integrated Security=True")
    Using cmd As New SqlCommand("SELECT UserId FROM Users WHERE Username=@u AND Password=@p", cn)
        cmd.Parameters.Add("@u", SqlDbType.VarChar, 50).Value = txtUserName.Text.Trim()
        cmd.Parameters.Add("@p", SqlDbType.VarChar, 128).Value = txtUserPass.Text.Trim()
        cn.Open()
        Using rdr = cmd.ExecuteReader()
            If rdr.Read() Then
                Session("userId") = rdr("UserId")
                Response.Redirect("AboutUs.aspx", False)
                Return
            End If
        End Using
    End Using
End Using

Troubleshooting and security checklist:

  • Verify the SQL Server service and correct instance name (named instances like SQLEXPRESS need Machine\Instance).
  • If using Integrated Security, ensure the app pool identity has DB permissions; otherwise use SQL auth.
  • Check table/column names for typos (the original query had a likely misspelling).
  • Avoid concatenating SQL (prevents SQL injection). Prefer parameterized queries or stored procedures.
  • Handle redirects so cleanup runs: use Response.Redirect(url, False) then Return, or rely on Using blocks as above.
  • Don’t store plaintext passwords. Move to hashed+salted passwords or use ASP.NET Identity/Membership for production authentication.
  • Add try/catch and logging to capture exceptions (connection errors, permission denied, invalid object name) rather than silently redirecting.

These steps address both the connection problem you hit and longer-term reliability and security. already fixed the immediate server name issue; the above will prevent future bugs and hard-to-find failures.

Recommended Answers

All 3 Replies

thx i just change my server name.

cool... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.