Database Login Part 2 (Logging in)   Leave a comment

Within this tutorial you will learn how to interact with the database we created in the first tutorial (here).

Fig 1 | Select datasourceFig 1 | Select datasource

Step 1) Setting up the project and forms.

Create a new windows form project in visual basic; on the pre set form create 2 textbox’s and name (Not change text) them “UsernameTextbox” and “PasswordTextbox“. Next add a button and call it “Submit“. You may wish to add labels. Create 2 new forms and call one “Normal” and the other “Admin“. While naming you may wish to change the first form to “Main“.

Fig 2 | New connectionFig 2 | New connection

Step 2) The database.

So first we need to connect the database to our application and to do this you go to (I am using visual studio 2010, 2008 may be different): Data>Add new datad source (fig 1).

Select database, then new dataset and then new connection. From here select you server as configed in section 1 and database as login (fig 2). Click ok and next and leave the

Fig 3 | New queryFig 3 | New query

name as set. Select next and select all the tables and click finish. 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 3). Select “Use SQL statement” > “Select witch returns a single value” > then press query builder. Fill out like in fig 4. Press ok and finish (call it instead of scalar query login), if an error box shows just discount it and its time to write some code.

Fig 4 | Query BuilderFig 4 | Query Builder

Step 3) The code.

From here you MUST save all, select the login form and select “debug”. When it loads close it down. You should see now in the toolbox you have some new toys, drag and drop “UsersTableAdapter1″ to your form and double click your button and write this code:

Dim Temp = UsersTableAdapter1.Login(UsernameTextbox.Text, PasswordTextbox.Text)
If Temp Is Nothing Then
MsgBox(“Check username and password”, MsgBoxStyle.Critical, “Error”)
Else
MsgBox(“Hello ” + UsernameTextbox.Text)
End If

Now if you run the program and try and enter a username and password it will return false. We need to add a username and password… to do this open up SQL management studio and select your login database table, in the object explorer window right click it and select “Edit top 200 rows” under username put what ever username you want, same applies for password but for privilege put 1. Press enter and try logging in again, it should welcome you with a message box.

Congratulations you have created your first login system, part 3 involves new users and privilege system

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: