22/09/2011

How to design a VB.net form for creating an unique user ID.

For better understanding  of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can  refer.
For creating an unique user id.Take following steps ;
1. Create a data table like following columns in ms access. naming "UserAccount"
     (i)   s.no                             -     number
     (ii)  Userid                         -    text           - make it primary key
     (iii) password                    -     text
      (iv) Remarks                    -     text
and else whatever details we want to take from the persons



.2 Open a window forms and place the following controls on it
      Text box as per requirement and
      to give the separate permissions like Admin and normal  to users we need to take   two radio button and the value of the click event of the radio button will be store in a test box and that text box should be placed such away that text box is not visible at run .time.
then place the  button control on the forms as you wish
      3. Take oledb data adapter and create a data connection with database and generate dataset by right clicking on data adapters. in above example the data adapter is DaUser and Dataset is DsUser and set the binding property of textbox  with data set.

4. then write following code in code editor:

Imports System.Data.OleDb

Public Class FrmUser
    Inherits System.Windows.Forms.Form
    Dim cn As New OleDbConnection
    Dim cmd As New OleDb.OleDbCommand
    Dim bm As BindingManagerBase
    Private Sub FrmUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PIS.mdb;Persist Security Info=True;Jet OLEDB:Database Password=123;"
 
            cn.Open()
            DaUser.Fill(DsUser1, "UserAccount")

            bm = Me.BindingContext(DsUser1, "UserAccount")
            GroupBox1.Enabled = False
            BtnAdd.Enabled = True
            BtnDelete.Enabled = True
            BtnSave.Enabled = False
            'RbtNormal.Checked = True
            If TxtAccounttype.Text = "A" Then
                RbtAdmin.Checked = True
            End If
            If TxtAccounttype.Text = "N" Then
                RbtNormal.Checked = True
            End If
            TxtPassword.UseSystemPasswordChar = True
        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 BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try
            GroupBox1.Enabled = True
            BtnSave.Enabled = True
            BtnAdd.Enabled = False
            BtnDelete.Enabled = True
            Dim eid As Integer

            If bm.Count = 0 Then
                eid = 1
            Else
                bm.Position = bm.Count - 1
                eid = CInt(TxtSno.Text) + 1
            End If
            bm.AddNew()
            clrtext()
            comboox()
            TxtSno.Text = eid.ToString
            BtnAdd.Enabled = False
            RbtNormal.Checked = True
            TxtAccounttype.Text = "N"


            GroupBox2.Enabled = True
            GroupBox3.Enabled = False
            TxtPassword.UseSystemPasswordChar = True
        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 comboox()
        CbRank.SelectedIndex = 0
           End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("Are You Sure", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Deleted") = MsgBoxResult.Yes Then
                If TxtSno.Text = "" Then
                    BtnDelete.Enabled = False
                Else
                    bm.RemoveAt(bm.Position)
                    DaUser.Update(DsUser1, "UserAccount")
                    MsgBox(" User ID Successfully deleted from this database")
                    BtnAdd.Enabled = True
                    BtnSave.Enabled = True
                    GroupBox3.Enabled = True
                    GroupBox1.Enabled = False
                    BtnAdd.Enabled = True
                End If
            End If
            TxtPassword.UseSystemPasswordChar = True
        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 BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If isValidData() Then
            Dim CmdER As New OleDbCommand
            Dim MyReader As OleDb.OleDbDataReader
            CmdER.Connection = cn
            CmdER.CommandText = "Select * from UserAccount where UserId = '" & TxtUserId.Text & "'"
            MyReader = CmdER.ExecuteReader
            If MyReader.Read() Then
                MsgBox("This user id is already claimed.Please select othe user Id")
                TxtUserId.Text = ""
                TxtUserId.Focus()
               Else
                Try
                    bm.EndCurrentEdit()
                    DaUser.Update(DsUser1, "UserAccount")
                    MsgBox(" User Id Successfully created")
                    BtnAdd.Enabled = True
                    BtnDelete.Enabled = True
                    BtnSave.Enabled = False
                    GroupBox3.Enabled = True
                    GroupBox1.Enabled = False
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End If
        TxtPassword.UseSystemPasswordChar = True
    End Sub

    Private Sub BtnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrevious.Click
        bm.Position -= 1
        BtnDelete.Enabled = True
        BtnAdd.Enabled = True
        BtnSave.Enabled = True
        GroupBox1.Enabled = False
        If TxtAccounttype.Text = "A" Then
            RbtAdmin.Checked = True
        End If
        If TxtAccounttype.Text = "N" Then
            RbtNormal.Checked = True
        End If
        TxtPassword.UseSystemPasswordChar = True
    End Sub

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
        bm.Position += 1
        BtnDelete.Enabled = True
        BtnAdd.Enabled = True
        BtnSave.Enabled = True
        GroupBox1.Enabled = False
        If TxtAccounttype.Text = "A" Then
            RbtAdmin.Checked = True
        End If
        If TxtAccounttype.Text = "N" Then
            RbtNormal.Checked = True
        End If
        TxtPassword.UseSystemPasswordChar = True
    End Sub
    Private Function isValidData() As Boolean
        Return _
             Class1.IsPresent(TxtSno) AndAlso _
             Class1.IsPresent(TxtUsername) AndAlso _
             Class1.IsSelected(CbRank) AndAlso _
                Class1.IsPresent(TxtUserId) AndAlso _
             Class1.IsPresent(TxtPassword) AndAlso _
            Class1.IsPresent(TxtAccounttype)

    End Function
    Private Sub clrtext()
        Dim cntrl As Control
        For Each cntrl In Me.Controls
            If TypeOf cntrl Is TextBox Then
                cntrl.Text = ""
            End If

        Next

    End Sub

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        bm.EndCurrentEdit()
        Me.Close()
    End Sub

    Private Sub RbtAdmin_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtAdmin.CheckedChanged
        TxtAccounttype.Text = "A"
        TxtPassword.UseSystemPasswordChar = True
    End Sub

    Private Sub RbtNormal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtNormal.CheckedChanged
        TxtAccounttype.Text = "N"
        TxtPassword.UseSystemPasswordChar = True
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        bm.CancelCurrentEdit()
        GroupBox1.Enabled = False
        BtnAdd.Enabled = True
        TxtPassword.UseSystemPasswordChar = True
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click


        If BtnEdit.Text = "&Recover Password" Then
            TxtPassword.UseSystemPasswordChar = False
            BtnEdit.Text = "&Hide Password"
        Else
            TxtPassword.UseSystemPasswordChar = True
            BtnEdit.Text = "&Recover Password"
        End If
    End Sub
End Class
I am a freelance programmer , I love programming so your comments will give me  motivation.
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
fHappy Coding


No comments:

Post a Comment

What & How