Imports System.Data
Imports System.Data.SqlClient
Partial Class Certification_Master
    Inherits System.Web.UI.Page
    Shared Search As Boolean
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            If (Not IsPostBack) Then        'If IsPostBack is false then
                ClearFields()               'Clear the TextFields
                UnLockTextBox(False)        'Unlock (Enable) TextFields
                g_User = "U1"
                g_Company = "COM1"
                Session("Financial") = "000010N"
                FillGrid("")                  'Fills the DatagGrid with All Records
                Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, g_User, "Certification_Master")
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub ClearFields()
        Try
            txtCertificationCode.Text = ""                           'Assign Null to the TextField
            txtCertificationName.Text = ""
            txtIssuedBy.Text = ""
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            txtCertificationCode.Enabled = Action                          'Assign Null to the TextField
            txtCertificationName.Enabled = Action
            txtIssuedBy.Enabled = Action
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub FillGrid(ByVal PTN As String)
        Try
            Dim qry As String                                   'Define a String Variable for Query
            Dim con As New SqlConnection                        'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                          'Open the Connection
            If Trim(PTN) = "" Then
                qry = "select Certification_Kid,Certification_Code,Certification_Name,Certification_IssuedBy from Certification_Master where Certification_IsDeleted='0'"
            Else
                qry = "select Certification_Kid,Certification_Code,Certification_Name,Certification_IssuedBy from Certification_Master where Certification_IsDeleted='0'"
            End If
            Dim dt As New DataTable
            dt = ReturnDataTable(qry)
            '    Dim cmd As New SqlCommand(qry, con)                 'Define command variable to execute the query
            '   Dim dr As SqlDataReader                             'Define DataReader variable for storing the output of the query
            '  dr = cmd.ExecuteReader                              'Assign the output of query to DataReader variable
            gvCertificate.DataSource = dt                          'Set the DataReader Variable as DataSource for the Grid
            gvCertificate.DataBind()                               'Bind the Data to the Grid
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub


    Protected Sub tvLinks_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    End Sub

    Protected Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(True)                                 'Enable the TextFields
            Call AddClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            txtCertificationId.Text = NewPrimaryKey(Me)
            txtCertificationCode.Text = Generate_Code("select count(*) from Certification_Master where Certification_Code='", "CC")
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If CheckValidData("Add") = True Then
                UpdateData("Insert")   'Inserts the Record
                CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
                ClearFields()                                                                   'Clear the TextFields
                UnLockTextBox(False)                                                                 'Disable the TextFields
                Call SaveClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Private Function CheckValidData(ByVal strSender As String) As Boolean
        If Trim(txtCertificationCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
            CreateMessageAlert(Me, "Certification Code Cannot be Null !", "StrKeyVal")      'Display the Message 
            Exit Function
        End If
        If Trim(txtCertificationName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
            CreateMessageAlert(Me, "Certification Name Cannot be Null !", "StrKeyVal")      'Display the Message
            Return False
            Exit Function
        End If
        If Trim(txtIssuedBy.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
            CreateMessageAlert(Me, "Certificate IssuedBy Text Cannot be Null !", "StrKeyVal")      'Display the Message
            Return False
            Exit Function
        End If
        Return True
    End Function

    Private Sub UpdateData(ByVal strMode As String)
        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
        sqlcoll.Add("@Certification_Kid", Data.SqlDbType.NVarChar, 10).Value = txtCertificationId.Text.Trim
        sqlcoll.Add("@Certification_Code", Data.SqlDbType.NVarChar, 25).Value = txtCertificationCode.Text.Trim
        sqlcoll.Add("@Certification_Name", Data.SqlDbType.NVarChar, 100).Value = txtCertificationName.Text.Trim
        sqlcoll.Add("@Certification_IssuedBy", Data.SqlDbType.NVarChar, 100).Value = txtIssuedBy.Text.Trim
        sqlcoll.Add("@Certification_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("Financial")
        sqlcoll.Add("@Certification_CompanyId", Data.SqlDbType.NVarChar, 10).Value = g_Company
        sqlcoll.Add("@Certification_UserId", Data.SqlDbType.NVarChar, 10).Value = g_User
        sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Page.ToString
        sqlcoll.Add("@Mode", Data.SqlDbType.Char, 10).Value = strMode.Trim
        If ExecQuery("Certification_Master_Proc", sqlcoll).StartsWith("True") Then
            FillGrid("")
            cmd = Nothing
            sqlcoll = Nothing
        End If
    End Sub

    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If CheckValidData("Update") = True Then
                UpdateData("Update")  'Updates the record
                CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
                ClearFields()                                                                   'Clear the TextFields
                UnLockTextBox(False)                                                            'Disable the TextFields
                Call EditClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            UpdateData("Delete")  'Deletes the record                                               'Execute the Query
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call DeleteClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
    Protected Sub gvCertificate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            txtCertificationId.Text = gvCertificate.SelectedRow.Cells(1).Text     'Assign the value of cell to the textField
            txtCertificationCode.Text = gvCertificate.SelectedRow.Cells(2).Text     'Assign the value of cell to the textField
            txtCertificationName.Text = gvCertificate.SelectedRow.Cells(3).Text       'Assign the value of cell to the textField
            txtIssuedBy.Text = gvCertificate.SelectedRow.Cells(4).Text
            UnLockTextBox(True)                                             'Enable the TextFields
            Call GridClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            Search = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                 'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(False)                                'Disable the TextFields
            Search = False
            txtCertificationName.AutoPostBack = False
            Call FillGrid("")
            Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            gvCertificate.SelectedIndex = -1
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

   
    Protected Sub gvCertificate_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvCertificate.PageIndexChanging
        gvCertificate.PageIndex = e.NewPageIndex
        FillGrid("")
        gvCertificate.SelectedIndex = -1
        ClearFields()
        Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
    End Sub

End Class
