1) Setting the privilege
This part is relativly simple, we are just going to extract the privilege using the user name as a condition. Just like in tutorial 2 add a new query, for those who forgot:
Click on the new “LoginDataSet.xsd”, from here you should see your table we created earlier in an ER diagram state (don’t worry if you don’t understand that bit, just a name for the diagram). From here click on the “UsersTableAdapter” and add new query (fig 1). Select “Use SQL statement” > “Select witch returns a single value” > then press query builder.
This time all we want is to select the privilege colum (fig 2). Name it “PrivilegeSelect” and save. Now we need to add some modifications to the code because its a bits scrappy, that code is below.
UsernameTextbox.ReadOnly = True ‘Sets the username textbox to readonly so no alterations can be made
PasswordTextbox.ReadOnly = True ‘Sets the password textbox to readonly so no alterations can be made
Dim Temp = UsersTableAdapter1.Login(UsernameTextbox.Text, PasswordTextbox.Text) ‘Runs the query
If Temp Is Nothing Then ‘Returns no matchs
UsernameTextbox.ReadOnly = False ‘Allows the user to now enter data into the username textbox
PasswordTextbox.ReadOnly = False ‘Allows the user to now enter data into the password textbox
MsgBox(“Check username and password”, MsgBoxStyle.Critical, “Error”) ‘Displays an error message
Else ‘If a user is found
Dim Privilege = UsersTableAdapter1.PrivilegeSelect(UsernameTextbox.Text) ‘runs password query using username as paramiter
Select Case Privilege
Case 0 ‘If the username is 0 the user will be redirected to Normal
Case 1 ‘If the username is 1 the user will be redirected to Admin
MsgBox(“An error has ocured within the privilage system. Please contact your system administrator!”, MsgBoxStyle.Critical, “Error”) ‘Displays an error in the case of an error
Catch ex As Exception ‘Error procortions
MsgBox(“An error has ocured: ” & ex.ToString, MsgBoxStyle.Critical)
The code is commented and from there on you can add what you wish to the user’s and administrators forms.
2) New user
For this part we need to create 2 new query’s one being a select to check if the user already exists and another to insert the new users credentials. The select query is simple, just do the exact same as privilege query but have it set for username instead of privilege;
call it “CheckUser”. For the next query we need to set it to insert instead of select, so on the secound window click select (fig 2). Build the query like fig 3 however this is also the default settings. From here we need to add a new form called “NewUser” and a button linking to it from ether admin or the login form (main), your choice. Just like the login form you need a username and password texbox called UsernameTextbox and ditto for password, a submit button and a “UserTableAdaptor”. The code for this is listed below (put in Submit Button):
Dim Check = UsersTableAdapter1.CheckUser(UsernameTextbox.Text)
If Check Is Nothing Then
UsersTableAdapter1.NewUser(UsernameTextbox.Text, PasswordTextbox.Text, “0″)
MsgBox(“New User created”, MsgBoxStyle.Information)
MsgBox(“Username allready exists”, MsgBoxStyle.Information)
There we go, run your application, populate your database and you are all done. Hope you enjoyed the tutorials and I will be making a lot more as the time comes.