School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,104 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,046 people online right now. Registration is fast and FREE... Join Now!




[WalkThrough] UserNames & Password Database

 
Reply to this topicStart new topic

> [WalkThrough] UserNames & Password Database, Picture Heavy. DataTableAdapter + Query

AdamSpeight2008
Group Icon



post 21 Aug, 2009 - 09:30 AM
Post #1


UserNames & Password Database

This seems a popular topic over the last few weeks, and it seem a lot get confused with ConnectionString and QueryStrings.
Wheeras if they utilise DataSet Qeuries there tasks will be a lot simpler

1. Creating the database.


In this tutorial I will using Microsoft Access to create my database, but is it possible use other method.
This part of the tutorial is the only section that will differ.

Create a new Database named UserPasswords

Create a new table called Users
Add UserID field Number not Autonumber
Add UserName field Text
Add Password field Text

Set UserID to be the primary key.
Attached Image
Save the table.

Lets add a few users

0 Administrator Administrator
1 User User
2 User2 abc

Attached Image
Save the table.

All done with creating the database now lets use it.


2. Importing the Database.

After creating a new windows form project.
Project -> Add Existing Item -> Find & Select the UserPasswords database.
Attached Image
Tick the Tables box -> Finish

This will the import the database in the project. It will also create an additional item in the solution explorer called UserPasswordsDataSet.xsd this is called a DataSet.
Attached Image

Save the project. In the next part will be using that dataset.


3. Adding a Query

It some point will need some capability to query the database to see if the username and password are valid. Let add part of that capability.

Double click on the dataset. This will open a new window with and overview of all the tables in our database. (Obviously there only in ours.)
Attached Image
You see the table has two sevtions;-
Users Which is are table from the database.
UsersTableAdapter

What's a TableAdapter? The job of a tableadapter is to translate our actions and queries into something the technology used by the database can understand.
If the database was using Microsoft Access then uses Access language, a SQL database uses SQL. It make our tasks and coding simpler. (You'll see.)

Right Click on the TableAdapter and select Add ->Add Query. -> Next
Attached Image

On this select SELCT which returns a single value, this is because we are don't need to return any of the rows to check the validity of a UserName and Password.

Click Next

Attached Image
If you know the SQL needed to accomplish the task enter it here.
Click Query Builder, open up the query builder window.
Attached Image
Modify the query so it looks like
Attached Image

What does =? mean? Simply you're saying to the query I'll tell you later so create a parameter for me to fill in.

Let test it. See in the bottom left a button with the text Execute Query. Click It
Attached Image

For some reason it doesn't display the parameter aliases, but it simple to figure that out there in order top to bottom as they appear in the query.
So in this case the first is UserName then Password.
For the first try; Administrator adminstrator
Click OK and you'll see we get result 1
Attached Image

Try it again but this time mis spell the password (or remove it completely) you see we get no results.
If fact we do it is NOTHING and this is an important point to remember.

We now finished building our query so click OK, this will now return to the form were we could enter the SQL directly. (You'll see it now has a different one.)
Click Next

Name the function ValidateUserNamePassword
Click Next -> Finished
Attached Image
And we're back in the DataSet, with the query added to the datatableadapter.


It this point it is a good idea and practice to save the project.
Next Part: Creating the form and Using the DataTableAdapter


4. Form Creation & Using a DataTable Adapter


On Form1 add two textboxes and a button. Let use good coding practices and give the controls better names than Textbox1.
So rename Textbox1 -> Txt_UserName , Textbox2 -> Txt_Password , Button1 -> But_Validate

Save

To enable use to add the TableAdapter to the Form, we need build the project. So build the project, one of the result of building is three (3) new controls in the toolbox.
Attached Image
Add a UsersTableAdapter to the form.

So far in this tutorial we not written a single line of vb.net code.
Double Click on the button.
CODE

  Dim r = Me.UsersTableAdapter1.v

In the intellisense you see.
Attached Image
Hang on is that the same name we entered for the query? And you correct. (Didn't it made are coding simpler)
CODE

Private Sub But_Validate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Validate.Click
Private Sub But_Validate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Validate.Click
  Dim r = Me.UsersTableAdapter1.ValidateUserNamePassword(Me.Txt_UserName.Text,Me.Txt_Password.Text)
  If r Is Nothing Then
   ' UserName / Password wrong
   MessageBox.Show("Invalid Username or Password")
  Else
   ' Valid Username & Password
   MessageBox.Show("Welcome")
  End If
End Sub


Why are checking the result r for NOTHING?
Remember in Query Builder when we tested, the possible results return were 1 (Valid User) or NOTHING (Incorrect UserName or Password)

Save and run the project, try it out.

5. Review
  1. We create the datebase.
  2. Add a few entries
  3. Imported the database
  4. Add a query to the dataset
  5. Created a simple login form
  6. Use a Data Table Adapter to translate then process our query,
  7. Wrote 8 lines of code (6 excluding the comments).
  8. Not had to deal with ConnectionStrings
  9. Not had to deal with QueryString
To me, I don't mined the additional step of adding a query to a DataSet to simplify the coding.

So now a your a DataSet welding Coding Ninja ph34r.gif

I'll do another tutorial soon covering SELECT rows, UPDATES and DELETE.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jatobtech
*



post 31 Aug, 2009 - 01:59 AM
Post #2
Hi, I regard this topic as perfect for the go 'cos i've been having problems dealing with this end of my project. But can U please help me out with creating user Name and Pass Word using SQL Server rather than microsoft Access.

Thanks for your assistance in this regards.
Tunde rolleyes.gif
Go to the top of the page
+Quote Post

kriaz
*



post 16 Oct, 2009 - 09:29 PM
Post #3
Very useful tutorial icon_up.gif ,

how ever there is a slight problem with your following code:

Dim r = Me.UsersTableAdapter1.ValidateUserNamePassword(Me.Txt_UserName.Text, Me.Txt_UserName.Text)

it should be ;

Dim r = Me.UsersTableAdapter1.ValidateUserNamePassword(Me.Txt_UserName.Text, Me.Txt_Password.Text)

kriaz
Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 16 Oct, 2009 - 11:23 PM
Post #4
Thank You for spotting that.
Edited Tutorial to include correction
Go to the top of the page
+Quote Post

TRSwaterloo
*



post 23 Oct, 2009 - 05:19 PM
Post #5
ph34r.gif skills on this tutorial, it was like wow I actually learned something and I didn't have to think.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 12:36PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month