
Imports System.Data
Imports System.Data.SqlClient
Partial Class Unit_Master
    Inherits System.Web.UI.Page
    Dim Search As Boolean
    Dim cmd As New SqlCommand
    Shared g_Company As String
    Shared g_FinYear As String
    Shared g_User As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If
        If Page.IsPostBack = False Then
            ClearFields()
            UnlockFields(False)
            'g_User = "UI"
            'g_Company = "COM1"
            'Session("Financial") = "000010N"
            g_Company = Session("g_Company").ToString()
            g_FinYear = Session("g_FinYear").ToString()
            g_User = Session("g_User").ToString()
            FillGrid("")
            BtnDelete.Attributes.Add("onclick", "return confirm('Are You Sure ! Want to Delete ?');")
            Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False              'Default Value for Search Mode
            Call CheckRights(Me, g_User, "Certification_Master")
        End If
    End Sub

    Protected Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try
            ClearFields()                                       'Clears the TextFields
            UnlockFields(True)                                 'Enable the TextFields
            Call AddClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtUnitId.Text = NewPrimaryKey(Me)
            txtUnitCode.Text = Generate_Code("select count(*) from Unit_Master where Unit_Code='", "UC")
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub UpdateData(ByVal StrMode As String)
        Dim sqlcol As Data.SqlClient.SqlParameterCollection = cmd.Parameters
        sqlcol.Add("@Unit_Kid", SqlDbType.NVarChar, 10).Value = txtUnitId.Text.Trim
        sqlcol.Add("@Unit_Code", SqlDbType.NVarChar, 25).Value = txtUnitCode.Text.Trim
        sqlcol.Add("@Unit_Name", SqlDbType.NVarChar, 50).Value = txtUnitName.Text.Trim
        sqlcol.Add("@Unit_Flag", SqlDbType.NVarChar, 50).Value = drpTypeOfUnit.SelectedItem.Text.Trim
        sqlcol.Add("@Unit_FinancialYearId", SqlDbType.NVarChar, 10).Value = Session("Financial")
        sqlcol.Add("@Unit_CompanyId", SqlDbType.NVarChar, 10).Value = g_Company
        sqlcol.Add("@Unit_UserId", SqlDbType.NVarChar, 10).Value = g_User
        sqlcol.Add("@FormName", SqlDbType.NVarChar, 50).Value = Me.Page.ToString
        sqlcol.Add("@Mode", SqlDbType.NVarChar, 10).Value = StrMode
        If ExecQuery("Unit_Master_Proc", sqlcol).StartsWith("True") Then
            FillGrid("")
            cmd = Nothing
            sqlcol = Nothing
        End If
    End Sub

    Protected Sub FillGrid(ByVal strID As String)
        Try
            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
            Dim strQuery As String
            If strID.Trim.Length = 0 Then
                strQuery = "select * from Unit_Master where Unit_IsDeleted = '0' AND Unit_CompanyId='" & Session("g_Company").ToString() & "' order by Unit_Name"
            Else
                strQuery = "select * from Unit_Master where Unit_IsDeleted = '0' AND Unit_Name like '" & strID.Trim & "%' AND Unit_CompanyId='" & Session("g_Company").ToString() & "' order by Unit_Name"
            End If

            Dim dt As DataTable = ReturnDataTable(strQuery)        ' Assign the output of query to DataTable variable
            gvUnitMaster.Columns(1).Visible = True
            gvUnitMaster.DataSource = dt                       'Set the DataTavke Variable as DataSource for the Grid
            gvUnitMaster.DataBind()                               'Bind the Data to the Grid
            gvUnitMaster.Columns(1).Visible = False
            gvUnitMaster.SelectedIndex = -1
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub


    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If Trim(txtUnitCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Unit Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtUnitName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Unit Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from Unit_Master where Unit_Name='" & txtUnitName.Text.Trim & "'and Unit_IsDeleted='0' and Unit_CompanyID='" & g_Company & "' and Unit_Flag='" & drpTypeOfUnit.SelectedItem.Text & "'") Then ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Unit Name Already Present !", "StrKeyVal")      'Display the Message
                txtUnitName.Focus()
                Exit Sub
            End If

            If CheckValidData("Update") = True Then
                UpdateData("Update")  'Updates the record
                CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
                ClearFields()                                                                   'Clear the TextFields
                UnlockFields(False)                                                            'Disable the TextFields
                Call EditClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            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
            UnlockFields(False)                                                            'Disable the TextFields
            Call DeleteClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
    Protected Sub BtnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Call SearchClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtUnitName.Enabled = True
            txtUnitName.AutoPostBack = True
            txtUnitName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try 'Set Focus to txt
    End Sub

    Protected Function CheckValidData(ByVal strSender As String) As Boolean
        If txtUnitName.Text.Trim = "" Then
            CreateMessageAlert(Me.Page, "Enter Unit Name", "strKeyVal")
            Exit Function
        End If
        Return True
    End Function

    Protected Sub UnlockFields(ByVal Action As Boolean)
        txtUnitId.Enabled = Action
        '  txtUnitCode.Enabled = Action
        txtUnitName.Enabled = Action
        drpTypeOfUnit.Enabled = Action
    End Sub

    Protected Sub ClearFields()
        txtUnitId.Text = ""
        txtUnitCode.Text = ""
        txtUnitName.Text = ""
        drpTypeOfUnit.ClearSelection()
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try

            If Trim(txtUnitCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Unit Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtUnitName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Unit Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from Unit_Master where Unit_Code='" & txtUnitCode.Text.Trim & "'and Unit_IsDeleted='0' and Unit_CompanyID='" & g_Company & "' and Unit_Flag='" & drpTypeOfUnit.SelectedItem.Text & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, "Unit Code Already Present !", "StrKeyVal")      'Display the Message
                txtUnitCode.Text = "" : txtUnitCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Unit_Master where Unit_Name='" & txtUnitName.Text.Trim & "'and Unit_IsDeleted='0' and Unit_CompanyID='" & g_Company & "' and Unit_Flag='" & drpTypeOfUnit.SelectedItem.Text & "'") Then ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Unit Name Already Present !", "StrKeyVal")      'Display the Message
                txtUnitName.Text = "" : txtUnitName.Focus()
                Exit Sub
            End If


            If CheckValidData("Insert") = True Then
                UpdateData("Insert")  'Updates the record
                CreateMessageAlert(Me, "Record Add Successfully...", "StrKeyVal")             'Display the Message
                ClearFields()                                                                   'Clear the TextFields
                UnlockFields(False)                                                            'Disable the TextFields
                Call SaveClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            End If
        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
            UnlockFields(False)                                'Disable the TextFields
            Search = False
            'txtStateName.AutoPostBack = False
            Call FillGrid("")
            gvUnitMaster.SelectedIndex = -1
            Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

  
    Protected Sub gvUnitMaster_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvUnitMaster.SelectedIndexChanged
        Try
            UnlockFields(True)
            ClearFields()
            txtUnitId.Text = gvUnitMaster.SelectedRow.Cells(1).Text.Trim
            txtUnitCode.Text = gvUnitMaster.SelectedRow.Cells(2).Text.Trim
            txtUnitName.Text = gvUnitMaster.SelectedRow.Cells(3).Text.Trim
            drpTypeOfUnit.ClearSelection()
            If gvUnitMaster.SelectedRow.Cells(4).Text <> "" And gvUnitMaster.SelectedRow.Cells(4).Text <> "&nbsp;" Then
                drpTypeOfUnit.Items.FindByText(gvUnitMaster.SelectedRow.Cells(4).Text.Trim).Selected = True
            End If
            Call GridClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub gvUnitMaster_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvUnitMaster.PageIndexChanging
        gvUnitMaster.PageIndex = e.NewPageIndex
        FillGrid("")
        UnlockFields(False)
        ClearFields()
        gvUnitMaster.SelectedIndex = -1
    End Sub

    Protected Sub txtUnitName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Call FillGrid(Trim(txtUnitName.Text))  'Fill the Grid filtering on UnitName

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
End Class
