Database Login Part 3 (New users and privilege)   Leave a comment

Within this tutorial you will learn how to add a privilege system and new user system into your programs. If you have not done so already please read tutorials one a two (tutorial 1, tutorial 2)

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:

Fig 1 | New queryFig 1 | New query

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

Try

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

Normal.Show()

Me.Close()

Case 1 ‘If the username is 1 the user will be redirected to Admin

Admin.Show()

Me.Close()

Case Else

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

End Select

End If

Catch ex As Exception ‘Error procortions

MsgBox(“An error has ocured: ” & ex.ToString, MsgBoxStyle.Critical)

End Try

The code is commented and from there on you can add what you wish to the user’s and administrators forms.

2) New user

Fig 2 | Insert windowFig 2 | Insert window

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;

Fig 3 | Insert query builderFig 3 | Insert query builder

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)
Else
MsgBox(“Username allready exists”, MsgBoxStyle.Information)
End If

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.

Posted July 16, 2010 by ngththien in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: