View Full Version : what code should i add to the following code to check invalid username or password


varshasan
03-17-2011, 01:34 AM
Public Class Login
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim str As String
Dim i As Integer


Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\varsha2011\plotdetails\plotdetails\login .mdb;")
'provider to be used when working with access database
cn.Open()
cmd = New OleDbCommand("select username, userpassword from login", cn)
dr = cmd.ExecuteReader
While dr.Read()
txtuser.Text = dr(0)
txtpass.Text = dr(1)
Me.Hide()
Dim mdi As New MDIParent1
mdi.Show()
MessageBox.Show("Wel-come " & txtuser.Text, "Wel-Come", MessageBoxButtons.OK)
End While
Catch
End Try
dr.Close()
cn.Close()
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
If MessageBox.Show("Are You sure?", "Exiting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
End
Else
Me.Show()
End If
End Sub

Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class

shamas21
04-14-2011, 05:52 AM
PublicClass Login
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim str AsString
Dim i AsInteger


PrivateSub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\varsha2011\plotdetails\plotdetails\login .mdb;")
'provider to be used when working with access database
cn.Open()
cmd = New OleDbCommand("select username, userpassword from login", cn)
dr = cmd.ExecuteReader
While dr.Read()
txtuser.Text = dr(0)
txtpass.Text = dr(1)
Me.Hide()
Dim mdi AsNew MDIParent1
mdi.Show()
MessageBox.Show("Wel-come " & txtuser.Text, "Wel-Come", MessageBoxButtons.OK)
EndWhile
Catch
EndTry
dr.Close()
cn.Close()
EndSub
PrivateSub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
If MessageBox.Show("Are You sure?", "Exiting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
End
Else
Me.Show()
EndIf
EndSub

PrivateSub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
EndSub
EndClass



Something like


Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim sPassword As String = ""
'Connect to the server
Using con As New System.Data.SqlClient.SqlConnection(cs)
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand
'Looks at the stored procedure
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
'Look at the login table
cmd.CommandText = "dbo.spLogin"
'Put in the parameters of the query, its looks at the username texbox
cmd.Parameters.Add("@Consultant", SqlDbType.NVarChar)
cmd.Parameters("@Consultant").Value = Trim(Me.tbxUsername.Text)
Using reader As SqlDataReader = cmd.ExecuteReader()
Dim table As New DataTable
table.Load(reader)
'Is there a row with the right credentials?
If table.Rows.Count = 0 Then
GoTo NotRecognised
End If
'Picks up the password
For Each row In table.Rows
sPassword = Trim((row(0).ToString()))
Next
End Using
con.Close()
End Using
Dim bLogin As Boolean = Trim(sPassword) = Trim(Me.tbxPassword.Text)
If bLogin = True Then
'Nice label stating its now connecting
Me.lblLoading.Text = "Connecting to the server. Please wait ....."
'Shows the homepage
frmHomePage.Show()
'Hides the login screen
Me.Visible = False
Exit Sub
End If
Me.Refresh()
NotRecognised:
'Message box stating the user is not recognised
MsgBox("You are not recognised by the system.")
End Sub