Hi, I am trying to develop a simple inventory system but I am having some problems now, and I need you guys' help.
I am trying to use the "accesslevel" to restrict users from accessing certain pages. I have 3 forms, form1 for login, form2 as mainpage and form3 as add new user/edit username/password page. I have the username,password and accesslevel(I only set it as either 1 or 2) database table on form3. The users with accesslevel 2 will be able to access form2, the one with accesslevel1 will go straight to form2 after logging in.
I am not sure what I have done wrong and I am not sure whether this is the correct way to restrict users from accessing certain pages or using certain functions of the system. I must admit that I am no good at all when it comes to coding. Please help me, thanks!
VB.NET:
Imports System.Data.SqlClient
Public Class Form1
Private Sub ValidateLogin(ByVal username As String, _
ByVal password As String, ByVal accesslevel As String)
Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\accounttlb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Dim sqlstring As String = String.Format("Select username,password,accesslevel from accpw where username = '{0}' and password ='{1}' and accesslevel ='{2}' ", tbusername.Text, tbpassword.Text, tbaccesslevel.Text)
Dim command As New SqlCommand(sqlstring, connection)
Dim myform As New Form3
Try
connection.Open()
If Not command.ExecuteScalar Is Nothing Then
For i As Integer = 0 To Me.AccpwDataGridView.Rows.Count - 1
If Me.AccpwDataGridView.Rows(i).Cells(2).Value = 2 Then
Me.Hide()
myform.Show()
ElseIf Me.AccpwDataGridView.Rows(i).Cells(2).Value = 1 Then
MessageBox.Show("Right, Logged In")
Me.Hide()
Form2.ShowDialog()
End If
Next
Else
MessageBox.Show("Wrong, Stay out")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Me.Close()
Finally
connection.Close()
End Try
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
ValidateLogin(Me.tbusername.Text, Me.tbpassword.Text, Me.tbaccesslevel.Text)
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'AccounttlbDataSet.accpw' table. You can move, or remove it, as needed.
Me.AccpwTableAdapter.Fill(Me.AccounttlbDataSet.accpw)
AccpwDataGridView.Hide()
End Sub
End Class
I am trying to use the "accesslevel" to restrict users from accessing certain pages. I have 3 forms, form1 for login, form2 as mainpage and form3 as add new user/edit username/password page. I have the username,password and accesslevel(I only set it as either 1 or 2) database table on form3. The users with accesslevel 2 will be able to access form2, the one with accesslevel1 will go straight to form2 after logging in.
I am not sure what I have done wrong and I am not sure whether this is the correct way to restrict users from accessing certain pages or using certain functions of the system. I must admit that I am no good at all when it comes to coding. Please help me, thanks!