Public Class frmApplicationUser
    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub LoadData()
        FillDataGrid("SELECT  USER_ID,  User_Name, Full_Name  FROM   ApplicationUser", DataGridView1)
    End Sub
    Private Sub frmApplicationUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        If txtName.Text = "" Or txtUserName.Text = "" Or txtPassword.Text = "" Or cmbUserType.Text = "" Then
            ErrorProvider1.SetError(txtName, "Mandatory")
            ErrorProvider1.SetError(txtUserName, "Mandatory")
            ErrorProvider1.SetError(txtPassword, "Mandatory")
            ErrorProvider1.SetError(cmbUserType, "Mandatory")
        Else
            ErrorProvider1.Clear()
            If btnSubmit.Text = "SAVE" Then
                ExecuteSQLQuery("SELECT *  FROM   ApplicationUser  WHERE  (User_Name = '" + str_repl(txtUserName.Text) + "')")
                If sqlDT.Rows.Count > 0 Then
                    ErrorProvider1.SetError(txtUserName, "The username already exists.")
                    MsgBox("The username already exists. Please use a different username.", MsgBoxStyle.Critical, "Critical")
                Else
                    ErrorProvider1.Clear()
                    ExecuteSQLQuery(" INSERT INTO ApplicationUser (Full_Name, Fathers_Name, User_Name, Password, Email, UserType) VALUES ('" + str_repl(txtName.Text) + "', '" + str_repl(txtFatherName.Text) + "', '" + str_repl(txtUserName.Text) + "', '" + str_repl(txtPassword.Text) + "', '" + str_repl(txtEmail.Text) + "', '" + str_repl(cmbUserType.Text) + "') ")
                End If
            ElseIf btnSubmit.Text = "UPDATE" Then
                ExecuteSQLQuery(" UPDATE  ApplicationUser SET  Full_Name='" + str_repl(txtName.Text) + "', Fathers_Name='" + str_repl(txtFatherName.Text) + "', User_Name='" + str_repl(txtUserName.Text) + "', Password='" + str_repl(txtPassword.Text) + "', Email= '" + str_repl(txtEmail.Text) + "', UserType= '" + str_repl(cmbUserType.Text) + "'  WHERE USER_ID=" & num_fltr(txtUserID.Text) & " ")
            End If

            txtUserName.Enabled = True
            txtUserName.Text = ""
            txtUserID.Text = ""
            txtPassword.Text = ""
            txtName.Text = ""
            txtFatherName.Text = ""
            txtEmail.Text = ""
            cmbUserType.Text = ""
            LoadData()
            btnSubmit.Text = "SAVE"
            MsgBox("Record has been saved successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 0 Then
            If MsgBox("Are you sure you want to edit this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                ExecuteSQLQuery("SELECT *  FROM ApplicationUser WHERE  USER_ID =" & DataGridView1.CurrentRow.Cells(2).Value & " ")
                If sqlDT.Rows.Count > 0 Then
                    txtUserName.Enabled = False
                    txtUserName.Text = sqlDT.Rows(0)("User_Name")
                    txtUserID.Text = DataGridView1.CurrentRow.Cells(2).Value
                    txtPassword.Text = sqlDT.Rows(0)("Password")
                    txtName.Text = sqlDT.Rows(0)("Full_Name")
                    txtFatherName.Text = sqlDT.Rows(0)("Fathers_Name")
                    cmbUserType.Text = sqlDT.Rows(0)("UserType")
                    txtEmail.Text = sqlDT.Rows(0)("Email")
                    btnSubmit.Text = "UPDATE"
                End If
            End If
        ElseIf e.ColumnIndex = 1 Then
            If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                ExecuteSQLQuery("DELETE FROM ApplicationUser WHERE  USER_ID =" & DataGridView1.CurrentRow.Cells(2).Value & " ")
                LoadData()
                MsgBox("Record deleted successfully.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub


End Class