huji
asked on
Login form
This is the code I'm using for a simple login form, which checks the entered username and password against a table in a remote database:
Imports System.Data.SqlClient
Public Class frmLogin
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim strLoginConnStr As String
strLoginConnStr = "Data Source=xxx;" & _
"Initial Catalog=xxx;User Id=xxx;Password=xxx;"
Dim connLogin As New SqlClient.SqlConnection(st rLoginConn Str)
connLogin.Open()
Dim strSQL As String = "sp_workshop_login '" & _
Replace(tbUsername.Text, "'", "''") & "', '" & _
Replace(tbPassword.Text, "'", "''") & "'"
Dim comLogin As New SqlCommand(strSQL, connLogin)
Dim Reader As SqlDataReader = comLogin.ExecuteReader()
If Reader.Read() Then
MsgBox("Good username and password!")
Else
MsgBox("Bad username or password!", MsgBoxStyle.Exclamation)
End If
connLogin.Close()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
I've got two questions in this regard:
1) Is this the best way to code a login form?
2) When the database is not available, the line connLogin.Open() will make the program hang. I even tried to surround it with a Try..Catch, but the exception doesn't occur when the database is down. How can I manage it to show an error message when the database is down?
Imports System.Data.SqlClient
Public Class frmLogin
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim strLoginConnStr As String
strLoginConnStr = "Data Source=xxx;" & _
"Initial Catalog=xxx;User Id=xxx;Password=xxx;"
Dim connLogin As New SqlClient.SqlConnection(st
connLogin.Open()
Dim strSQL As String = "sp_workshop_login '" & _
Replace(tbUsername.Text, "'", "''") & "', '" & _
Replace(tbPassword.Text, "'", "''") & "'"
Dim comLogin As New SqlCommand(strSQL, connLogin)
Dim Reader As SqlDataReader = comLogin.ExecuteReader()
If Reader.Read() Then
MsgBox("Good username and password!")
Else
MsgBox("Bad username or password!", MsgBoxStyle.Exclamation)
End If
connLogin.Close()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
I've got two questions in this regard:
1) Is this the best way to code a login form?
2) When the database is not available, the line connLogin.Open() will make the program hang. I even tried to surround it with a Try..Catch, but the exception doesn't occur when the database is down. How can I manage it to show an error message when the database is down?
2) you can shorter the timeout property of the Command object.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>> ... "Connect Timeout=5" ...
It did the trick.
>> 2. Use "using" if you're in VB2005.
What is its benefit in this case?
It did the trick.
>> 2. Use "using" if you're in VB2005.
What is its benefit in this case?
2. Using disposes the objects for you. In the case of connection objects, it closes them for you as well.
ASKER