19/09/2011

How to create a unique login and password in VB.Net programming


To make an aplication more secure we need to create a login where only authorise persons can use your application. So lets create a loging form for that we need following control 


  1.  
  2. create a data table in your database naming UserAccount with following column


    (i) sno-     number
    (ii)Userid- text- primary key
    (iii)password-text
  2.  Then on form take following control on your forms
(i)     label for User Id
(ii)    Label for Password
(iii)  Tetxbox for User ID
(iv)    Textbox for Password
(v)     Bottn for ok
(vi)    Botton for cancel
(v) oledbdataadapter and connect to your database
(vi) create a dataset with the help of dataadapter
(vii)  Bind the text controls textbox puserid and password with dataset.
(viii)Then writedown following code on formload, okbtton click and cancel botton click
Imports System.Data.OleDb
Imports System.IO

Public Class FrmLogin
    Inherits System.Windows.Forms.Form
    Dim cn As New OleDbConnection
    Dim cmd As New OleDb.OleDbCommand
    Public LoginSucceeded As Boolean
    Dim Counter As Integer
    Dim bm As BindingManagerBase
    Private Sub FrmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn.ConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PIS.mdb;Persist Security Info=True;Jet OLEDB:Database Password=123;"
        cn.Open()
        LoginSucceeded = False
    End Sub

    Private Sub BtnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOk.Click
        Try
            If Counter = 4 Then
                MsgBox("You have exceeded the number of attempts")
                SplashScreen1.Close()
                Me.Close()
                Exit Sub
            End If
            If TxtUserId.Text = "" Or TxtPassword.Text = "" Then
                MsgBox("You must enter a user name and password.", _
                vbOKOnly + vbInformation)
                TxtUserId.Focus()
            ElseIf TxtUserId.Text = "irbn" And TxtPassword.Text = "irbn@1234" Then
                FrmMainMDI.Show()
                Me.Hide()
              

            Else

                Dim CmdER As New OleDbCommand
                Dim MyReader As OleDb.OleDbDataReader
                CmdER.Connection = cn
                CmdER.CommandText = "Select * from UserAccount where UserId = '" & TxtUserId.Text & " 'and UserPassword =  '" & TxtPassword.Text & "'"
                MyReader = CmdER.ExecuteReader
                If MyReader.Read() Then
                    TxtUserAccout.Text = MyReader("AccountType")
                    If TxtUserAccout.Text = "A" Then
                        FrmMainMDI.Show()
                        Me.Hide()
                        FrmMainMDI.DatabackUpToolStripMenuItem.Enabled = False
                        Me.log()
                       
                    Else
                        FrmMainMDI.Show()
                        Me.Hide()
                        FrmMainMDI.UtilityToolStripMenuItem.Enabled = False
                        Me.log()
                       
                    End If
                    LoginSucceeded = True
                ElseIf MsgBox("Wrong user name or password") Then
                    TxtUserId.Text = ""
                    TxtPassword.Text = ""
                    TxtPassword.Focus()
                    Counter = Counter + 1
                    If MsgBox("Invalid Password, try again!", , "Login") Then
                        TxtUserId.Text = ""
                        TxtPassword.Text = ""
                        TxtUserId.Focus()
                        Counter = Counter + 1
                    End If
                End If
            End If
        Catch ex As Exception
            Dim MessageString As String = "Report this error to the system administrator: " & ControlChars.NewLine & ex.Message
            Dim TitleString As String = "Employee Master Details Data Load Failed"
            MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        SplashScreen1.Close()
        Me.Close()

    End Sub
To understand Vb.Net in better way I will suggest you to have  book written by Anne Prince  "Murach Visual Basic" and that is available on  Amozon and Flipkart