Thursday, 4 August 2011

VB User User Authentication using SQL

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”)
Click on Image to Enlarge
 
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”

Step Three: Create “Login” and “Main” Form





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


Code

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



Tuesday, 2 August 2011

A Simple VB 2008 Picture Slide Show

Its very easy to Create a VB 2008/2010 Picture Slide Show all you use is;a Timer, PictureBox, OpenFileDialogBox and a List Box.

You will
  1. Work with a Timer.
  2. Add Items to a listbox
  3. Remove a specific Item from a listbox
  4. Remove all items from a listbox(Clear)
  5. Using an OpenFileDialog box
  6. and working with MessageBox Results
Step 1: Design
Create a VB Form and in it, insert a Tab Controler with two tabs. One tab for the slide show, and the other tab for the settings of the slide show. 



Below is a Table showing details of the Form and the Other Objects
 

Object
Property
Settings
Function
Form
Name:
Text:
MaximiseBox:
StartPosition:

frmSlide
“Slide Show”
False
CenterScreen

Slide Show
Timer
Name:
Interval:
tmrPicTimer
1000

Changes image after 1 second
OpenFileDialog
Name:
FileName:

OpenPic
“ ”
Browse Computer for images
TabControl
Name:
TabPages:
mainTab
tabShow, tabSettings

Multiple tabs in one form to avoid using many Forms
PictureBox
Name:
BorderStyle:
SizeMode:
picSlideShow
FixedSingle
StretchImage

Show Images/Slides
Button
Name:
Text:
btnStart
 “Start”

Starts Show/Timer
Button
Name:
Text:
btnStop
 “Stop”

Stops Show/Timer
Button
Name:
Text:
btnExit
 “Exit”

Exits/Quits Application
ListBox
Name:

lstPictures

List of Pictures Added by user
Button
Name:
Text:
btnAdd
 “Add”

Adds Images into lstPictures as items from Dialog box
Button
Name:
Text:
btnRemove
 “Remove”

Removes Selected Item/pic from lstPictures
Button
Name:
Text:
btnClear
 “Clear”

Clears all items/pictures from lstPictures


Step 2: Code
Public Class Form1
   
Dim PicNum, Counter As Integer
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
tmrPicTimer.Enabled = True
        tmrPicTimer.Start()
End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        tmrPicTimer.Enabled = False
End Sub

Private Sub tmrPicTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles                 tmrPicTimer.Tick
        PicNum = lstPictures.Items.Count
        If Counter < PicNum Then
                picSlideShow.Image = Image.FromFile(lstPictures.Items.Item(Counter))
                Counter = Counter + 1
        Else
                Counter = 0
                picSlideShow.Image = Image.FromFile(lstPictures.Items.Item(Counter))
        End If
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        lstPictures.Items.Clear()
End Sub

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstPictures.Items.RemoveAt(lstPictures.SelectedIndex)
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If OpenPic.ShowDialog() = Windows.Forms.DialogResult.OK Then
                lstPictures.Items.Add(OpenPic.FileName)
        End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click                      
        If MsgBox("Are you Sure you want to Quit?", MsgBoxStyle.YesNo, "Quit") = MsgBoxResult.Yes Then
                Application.Exit()
        End If
End Sub

End Class