Here is what am thinking; if i have a table of users, then anytime you query it for a any set of 'username' and 'password' using the select statement, number of rows in your query result will either be 1(for a valid set) or 0(for an invalid set).
Lets do it in VB
Step One: Add Service Based Database to your Project
1. Click “Project”, Choose “Add New Item”
2. Select “Service-based Database”
3. Give your Database a name and Click “Add” (Database Name in Example is “ApplicaionDB”)
4. A Data Source Configuration will appear. Click “Cancel”, Since we have no Tables in the new Database
![]() |
Click on Image to Enlarge |
5. Database you created will appear on your “Server Explorer” Tab, Under “Data Connections”.
6. Double Click on your Database (ApplicationDB.mdf)
7. Right Click on “Tables” and Choose “Add Table”
8. A Table Definition Tab will appear, Create as Defined below
9. To Save the Table, Right Click on the Table Definition Tab and “Save Table1”
10. An Input Box will appear, prompting table name. Save table as “Users”
11. Right click on the Table Definition Tab and Close it(Click Close)
12. Your Table should be visible in the “Sever Explorer” tab
Step Two: Get Database “Connection String”
1. Click “Data”, Choose “Add New Data Source”
2. A Data Source Configuration will appear. Click “Database”, then “Next
![]() |
Click on Image to Enlarge |
3. Click “Next” again
4. The Next Dialog box will show you a list on databases you have on your system, Choose the one your created (ApplicationDB.mdf)
5. On the same Dialog Box, The last section should be labeled “Connection string”
6. Click on the “+” sign to expand it, and your connection string will be displayed
![]() |
Click on Image to Enlarge |
7. Copy and save your Connection String. It should be something like this;
“Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ApplicationDB.mdf;Integrated Security=True;User Instance=True”
8. Click Cancel, Since we didn’t want to create any Datasets, We just wanted to get a “Connection String”
Object | Property | Settings | Function |
Form | Name: Text: Start Possition: | Form1 “Login” CenterScreen | User Authentication Form |
TextBox | Name: | UsernameTextBox | User Login ID |
TextBox | Name: PasswordChar: | PasswordTextBox “*” | User Login Password |
Button | Name: Text: | btnOK “OK” | Performs Authentication(tests if Username and Password are Valid) |
Button | Name: Text: | btnCancel “Cancel” | Quits Application. |
Form | Name: Text: Start Possition: | Main “Main Form” CenterScreen | Main Form to be Displayed if Login is Successful |
Public Class LoginForm
' TODO: Perform custom authentication using the provided username and password
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
Dim MyDbConnection As New SqlClient.SqlConnection
Dim MySqlCommand As New SqlClient.SqlCommand
Dim MyAdapter As New SqlClient.SqlDataAdapter
Dim MyDataset As New DataSet
'Add Connection String (See Pages 4 and5 on how to get your connection string)
MyDbConnection.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ApplicationDB.mdf;Integrated Security=True;User Instance=True")
MySqlCommand.CommandText = "SELECT * From [Users] WHERE UserName= '" & UsernameTextBox.Text & "' AND Password='" & PasswordTextBox.Text & "' "
'Open Connection and Query the Database
MyDbConnection.Open()
MySqlCommand.Connection = MyDbConnection
MyAdapter.SelectCommand = MySqlCommand
MyAdapter.Fill(MyDataset, "0")
'There Can only be one user with the Entered Username and Password
Dim Count = MyDataset.Tables(0).Rows.Count
If Count = 1 Then 'Table can only have 1 user with that Username and Password
Main.Show() 'Form Show When Username and Password are Valid
Me.Hide()
Me.Visible = False
PasswordTextBox.Clear()
UsernameTextBox.Clear()
Else
MsgBox("Please Check Your Username and/or Password and Try Again", MsgBoxStyle.Critical, "Login Failed")
PasswordTextBox.Clear()
UsernameTextBox.Focus()
End If
MyDbConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Application.Exit()
End Sub
End Class