Public Class Login
' Place this code under your Public Class. If your form name is Login, then place it under Public Class Login
Inherits System.Windows.Forms.Form
Dim iCount As Integer ' this integer is declared to help count the number of imes a user tried to login.
Dim frmMain As New Form1 ' this line declares a variable that will point the user to the Main Screen upon a successful login. The second form created is called MainScreen.vb
'Dim UserLoginInfo As New user
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loginbtn.Click
Dim theConnectionString As New SqlClient.SqlConnection("Server=.\SQLExpress;AttachDbFilename=C:\AITP NEW\AITP\AITP\users.mdf;Database=users;Trusted_Connection=Yes;")
Dim theSqlCommand As New SqlClient.SqlCommand("SELECT [User_LoginType] FROM [Users] WHERE [User_Name] = @username AND [User_Password] =@password", theConnectionString)
Dim theUsername As New SqlClient.SqlParameter("@username", SqlDbType.VarChar, 50)
theUsername.Value = Usernametxbx.Text
Dim thePassword As New SqlClient.SqlParameter("@password", SqlDbType.VarChar, 50)
thePassword.Value = Passwordtxbx.Text
theSqlCommand.Parameters.Add(theUsername)
theSqlCommand.Parameters.Add(thePassword)
'open the connection
theSqlCommand.Connection.Open()
Dim theDataReader As SqlClient.SqlDataReader = theSqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
Dim theLoginType As Object = Nothing
If theDataReader.HasRows Then
theDataReader.Read()
'the user exists!! Lets get the logintype:
theLoginType = theDataReader("User_LoginType")
'UserLoginInfo.UserType = theLoginType
'UserLoginInfo.user_info = theUsername
End If
theSqlCommand.Connection.Close()
If theLoginType = "admin" Then
If accesslvlcmbx.SelectedItem = "User" Then
Me.Hide()
frmMain.Show()
Me.Close()
ElseIf accesslvlcmbx.SelectedItem = "Administrator" Then
Me.Hide()
admin.Show()
Me.Close()
ElseIf accesslvlcmbx.SelectedItem = "" Then
MessageBox.Show("You need to select a login type.", "LoginType", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
ElseIf theLoginType = "user" Then
If accesslvlcmbx.SelectedItem = "User" Then
Me.Hide()
frmMain.Show()
Me.Close()
ElseIf accesslvlcmbx.SelectedItem = "Administrator" Then
MessageBox.Show("You do not have sufficient access. Please select again.", "LoginType", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf accesslvlcmbx.SelectedItem = "" Then
MessageBox.Show("You need to select a login type.", "LoginType", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class