Imports System.Data.SqlClient
Public Class frmAddUser
      Private Sub frmAddUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btn_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       
    End Sub
    Public Sub Userlog()
        Dim dd As String
        dd = Now.Date
        Try
            Call opencon()
            Dim myd As New SqlDataAdapter
            With myd
                .InsertCommand = New SqlCommand
                .InsertCommand.CommandText = "INSERT INTO loy(datte,uname,action)VALUES(@datte,@uname,@action) "
                .InsertCommand.Connection = con
                With .InsertCommand.Parameters
                    .AddWithValue("@datte", dd)
                    .AddWithValue("@uname", logger)
                    .AddWithValue("@action", action)
                End With
                .InsertCommand.ExecuteNonQuery()
            End With
            Call closecon()
        Catch ex As Exception
            MsgBox(ex.Message)
            Call closecon()
        End Try
    End Sub

    Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

   
    Public Sub clear()
        Me.txtname.Clear()
        txtAddress.Clear()
        txtconf.Clear()
        txtfn.Clear()
        txtln.Clear()
        txtpass.Clear()
        mtxtpno.Clear()
        Me.cbodes.SelectedIndex = -1
    End Sub

   

    Private Sub txtconf_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.txtpass.Text = Me.txtconf.Text Then

        Else
            MsgBox("Password do not match.....", MsgBoxStyle.Exclamation, "WARNING")
            Me.txtconf.Focus()
            Me.txtconf.SelectAll()
        End If
    End Sub

   
    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click
        Me.Close()
    End Sub

    Private Sub btn_Edit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Edit.Click
        If Me.txtname.Text.Length = 0 Then
            MsgBox("Please enter the username to update the password", MsgBoxStyle.Information, "ALERT")
            Exit Sub
        End If
        nam = Me.txtname.Text
        Call clear()
        Me.Hide()
        ChangePass.Show()
    End Sub

    Private Sub btn_Delete_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click
        If Me.txtname.Text.Length = 0 Or Me.txtpass.Text.Length = 0 Or txtAddress.Text.Length = 0 Or txtfn.Text.Length = 0 Or txtln.Text.Length = 0 Then
            MessageBox.Show("Required detialed information about the record", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If MsgBox("Are you sure you want to delete record(s)...", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2) = MsgBoxResult.Yes Then
            Try
                Call opencon()
                Dim myd As New SqlDataAdapter
                With myd
                    .DeleteCommand = New SqlCommand
                    .DeleteCommand.CommandText = "DELETE FROM login WHERE username LIKE '%%" & txtname.Text & "%%'"
                    .DeleteCommand.Connection = con
                    .DeleteCommand.ExecuteNonQuery()
                End With

                Call closecon()

                MsgBox("Record successsfully deleted....", MsgBoxStyle.Information, "Deleted...")
                Call clear()
            Catch ex As Exception
                MsgBox(ex.Message)
                Call closecon()

            End Try
        Else
            MsgBox("Deletion cancelled....", MsgBoxStyle.Information, "Cancelled....")
        End If
    End Sub

    Private Sub btn_Refresh_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Refresh.Click
        Call clear()
    End Sub

    Private Sub btn_close_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_close.Click
        If Me.txtname.Text.Length = 0 Then
            MessageBox.Show("Please enter the name to search for...", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        Else
            Try
                Call opencon()
                Dim myda As New SqlDataAdapter
                With myda
                    .SelectCommand = New SqlCommand
                    .SelectCommand.CommandText = "SELECT * FROM login WHERE username LIKE '%%" & Me.txtname.Text & "%%'"
                    .SelectCommand.Connection = con
                End With
                Dim sql As SqlDataReader = myda.SelectCommand.ExecuteReader
                Dim dt As New DataTable
                dt.Load(sql)
                Dim row As DataRow = dt.Rows(0)
                Me.cbodes.Text = CStr(row("designation"))
                Me.txtname.Text = CStr(row("username"))
                Me.txtpass.Text = CStr(row("password"))
                Me.txtconf.Text = CStr(row("password"))
                Me.txtAddress.Text = CStr(row("address"))
                Me.txtfn.Text = CStr(row("fname"))
                Me.txtln.Text = CStr(row("lname"))
                Me.mtxtpno.Text = CStr(row("pnumber"))
                Call closecon()
            Catch ex As Exception
                MsgBox(ex.Message)
                Call closecon()
            End Try
        End If
    End Sub

    Private Sub txtfn_Leave1(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtfn.Leave
        Me.txtfn.Text = StrConv(Me.txtfn.Text, vbProperCase)
    End Sub

    Private Sub txtln_Leave1(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtln.Leave
        Me.txtln.Text = StrConv(Me.txtln.Text, vbProperCase)
    End Sub

    Private Sub txtAddress_Leave1(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAddress.Leave
        Me.txtAddress.Text = StrConv(Me.txtAddress.Text, vbProperCase)
    End Sub

    Private Sub btn_Add_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Add.Click
        If Me.txtname.Text.Length = 0 Then
            MsgBox("Please enter username....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.txtpass.Text.Length = 0 Then
            MsgBox("Please enter Password....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.txtconf.Text.Length = 0 Then
            MsgBox("Please enter confirmation password....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.txtAddress.Text.Length = 0 Then
            MsgBox("Please enter address....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.txtfn.Text.Length = 0 Then
            MsgBox("Please enter firstname....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.mtxtpno.Text.Length = 0 Then
            MsgBox("Please enter phone number....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.txtln.Text.Length = 0 Then
            MsgBox("Please enter lastname....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        ElseIf Me.cbodes.Text.Length = 0 Then
            MsgBox("Please select designation from the dropdown....", MsgBoxStyle.Information, "Hotels..")
            Exit Sub
        Else

            Call opencon()
            Dim myda As New SqlDataAdapter
            With myda
                .SelectCommand = New SqlCommand
                .SelectCommand.CommandText = "SELECT * FROM login WHERE username LIKE '%%" & Me.txtname.Text & "%%'"
                .SelectCommand.Connection = con
            End With
            Dim dr As New DataSet
            myda.Fill(dr, "myTable")
            If dr.Tables("myTable").Rows.Count > 0 Then
                MsgBox("Username already exist...", MsgBoxStyle.Exclamation, "Alert...")
                Me.txtname.Focus()
                Me.txtname.SelectAll()
                Exit Sub
            Else

                Try
                    Call opencon()
                    Dim myd As New SqlDataAdapter
                    With myd
                        .InsertCommand = New SqlCommand
                        .InsertCommand.CommandText = "INSERT INTO login(designation,username,password,address,fname,lname,pnumber)VALUES(@designation,@username,@password,@address,@fname,@lname,@pnumber) "
                        .InsertCommand.Connection = con
                        With .InsertCommand.Parameters
                            .AddWithValue("@designation", Me.cbodes.Text)
                            .AddWithValue("@username", Me.txtname.Text)
                            .AddWithValue("@password", Me.txtpass.Text)
                            .AddWithValue("@address", Me.txtAddress.Text)
                            .AddWithValue("@fname", Me.txtfn.Text)
                            .AddWithValue("@lname", Me.txtln.Text)
                            .AddWithValue("@pnumber", Me.mtxtpno.Text)
                        End With
                        .InsertCommand.ExecuteNonQuery()
                    End With
                    Call closecon()
                   
                    MsgBox("Record successfully saved...", MsgBoxStyle.Information, "Saved....")
                    Call clear()
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Call closecon()
                End Try
            End If

        End If

    End Sub
End Class