Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a Login Form in which when ADMIN is logged in he has all rights on Application, but when EMPLOYEE is logged in with different UserName and PassWord he has different access. Like Employee can just ADD something and other buttons on form are disable.

What I have tried:

VB
Private Sub loginbtn_Click_1(sender As System.Object, e As System.EventArgs) Handles loginbtn.Click
        For Each ctrl As Control In Me.Controls
            If (usernametxt.Text & passwordtxt.Text = String.Empty) Then
                ErrorProvider1.SetError(usernametxt, "Incorrect")
                ErrorProvider2.SetError(passwordtxt, "Incorrect")
                MsgBox("Fill The Details", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
        Next
        con.Open()
        l = "Select username, password, type From Login where username='" & usernametxt.Text & "' and password='" & passwordtxt.Text & "'"
        com = New SqlCommand(l, con)
        dr = com.ExecuteReader
        dr.Read()
        If usernametxt.Text = dr.GetString(0) Then
            If passwordtxt.Text = dr.GetString(1) Then
                If dr.GetString(2) = "ADMIN" Then
                    Me.Hide()
                    HomePage.Show()
                ElseIf dr.GetString(2) = "EMPLOYEE" Then
                    Me.Hide()
                    HomePage.Show()
                    StudentReg.editbtn.Enabled = False
                    StudentReg.removebtn.Enabled = False
                    StaffReg.editbtn.Enabled = False
                    StaffReg.removebtn.Enabled = False
                    HomePage.CourseFeesToolStripMenuItem.Enabled = False
                    HomePage.BatchesToolStripMenuItem.Enabled = False
                    HomePage.EventsToolStripMenuItem.Enabled = False
                    Billing.Button1.Enabled = False
                    HomePage.ReportsToolStripMenuItem.Enabled = False
                End If
            End If
        Else
            MsgBox("Please Enter Correct Username And Password", MsgBoxStyle.Critical)
        End If
        usernametxt.Text = ""
        passwordtxt.Text = ""
        con.Close()
    End Sub
Posted
Updated 29-Feb-16 19:17pm
v2
Comments
Ashutosh Dutondkar 29-Feb-16 10:36am    
I have try doing above code. Its working all right but when form is Close and Open the Buttons are enable. I dont know how to declare global variables in VB.NET please help me with the code how to declare globally so I can use it on other fomrs as well.
Aria Jafarian 29-Feb-16 11:10am    
Check the solution for Global variables below.
Ashutosh Dutondkar 29-Feb-16 12:54pm    
But how should I implement my above code in the solution. I'm unable to do the coding. Please help me with the implementation of above code!
Ashutosh Dutondkar 29-Feb-16 13:25pm    
I don't want two forms for ADMIN and EMPLOYEE. It should be checked on same form. I don;t have doubt in my code, because its working properly! Just I want to know how can i declare it globally! So I can check it on other forms! I have table in my database with columns username, password and type. Type has two rows "ADMIN" and "EMPLOYEE"
Aria Jafarian 29-Feb-16 13:32pm    
Where you check if your user is an Admin or Employee just add this
GlobalVariables.IsAdmin = True (obviously this is when your user is Admin)

I just added it for you in your code:
If dr.GetString(2) = "ADMIN" Then
GlobalVariables.IsAdmin = True
Me.Hide()
HomePage.Show()
ElseIf dr.GetString(2) = "EMPLOYEE" Then
GlobalVariables.IsAdmin = False
Me.Hide()
HomePage.Show()


and add this class to your project
Public Class GlobalVariables
Public Shared Property IsAdmin As bool
End Class

Then everywhere in your application you have access to GlobalVariables.IsAdmin

1 solution

C#
There are a couple of ways to do this in VB: a VB-specific way and a non-VB specific way (i.e. one that could also be implemented in C#.

The VB-specific way is to create a module and place the variable in the module:

Public Module GlobalVariables
   Public MyGlobalString As String
End Module

The non-VB-specific way is to create a class with shared properties:

Public Class GlobalVariables
  Public Shared Property MyGlobalString As String
End Class

The primary difference between the two approaches is how you access the global variables.

Assuming you are using the same namespace throughout, the VB-specific way allows you to access the variable without a class qualifier:

MyGlobalString = "Test"

For the non-VB-specific way, you must prefix the global variable with the class:

GlobalVariables.MyGlobalString = "Test"

Although it is more verbose, I strongly recommend the non-VB-specific way because if you ever want to transition your code or skillset to C#, the VB-specific way is not portable.


[^]

Credit
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900