Imports System.Data
Imports System.Data.SqlClient
Imports ModCommon
Partial Class other_details
    Inherits System.Web.UI.Page
    Shared Category(10) As String
    Shared i As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        lblClientField.Visible = False
        lblOptionalMsg.Visible = False
        lblBillingMsg.Visible = False
        lblIncludedMsg.Visible = False
        lblSparePartsMsg.Visible = False
        If Session("SupplierID") Is Nothing Then
            Response.Redirect("listproduct.aspx?sess=expire")
            Exit Sub
        End If
        If Not IsPostBack Then
            If Session("ProductID") = Nothing Then ' Check whether Session Productid 
                Response.Redirect("List_Product.aspx")
            End If
            i = 0
            Dim catid As String
            catid = ReturnValue("Select Product_CategoryID from Product_Master where Product_Kid= '" & Session("ProductID").ToString() & "'and Product_IsDeleted='0' and Product_FinancialYearId ='" & Session("g_FinYear").ToString & "' and Product_CompanyId ='" & Session("g_Company").ToString & "'")
            ' get categoryid according productid
            FillCategoryName(catid)

            If Session("Index") <> Nothing Then
                Mtv.ActiveViewIndex = CType(Session("Index").ToString, Integer)
                'Menu1.SelectedItem.Value = Mtv.ActiveViewIndex.ToString()
                getexisting()
                'Session("Index") = Mtv.ActiveViewIndex
                'HiddenField1.Value = Mtv.ActiveViewIndex
                'Session("Index") = Nothing
            Else
                Mtv.ActiveViewIndex = 0
                Session("Index") = "0"
                'HiddenField1.Value = 0
            End If
            'If ddlBillingFrom.SelectedItem.Text = "Add New Factory" Then
            'ddlBillingFrom.Attributes.Add("OnSelectedIndexChanged", "return DeleteMsg();")
            'End If
            btnBillingDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            btnClientDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            btnIncludedAcceDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            btnOptionAccDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            btnSparePartsDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")

            'btnBillingSave.Attributes.Add("onclick", "return SaveConfirm();")
            'btnClientSave.Attributes.Add("onclick", "return SaveConfirm();")
            'btnIncludedAcceSave.Attributes.Add("onclick", "return SaveConfirm();")
            'btnOptionAccSave.Attributes.Add("onclick", "return SaveConfirm();")
            'btnSparePartsSave.Attributes.Add("onclick", "return SaveConfirm();")

            btnBillingUpdate.Attributes.Add("onclick", "return UpdateMsg();")
            btnClientUpdate.Attributes.Add("onclick", "return UpdateMsg();")
            btnIncludedAcceUpdate.Attributes.Add("onclick", "return UpdateMsg();")
            btnOptionAccUpdate.Attributes.Add("onclick", "return UpdateMsg();")
            btnSparePartsUpdate.Attributes.Add("onclick", "return UpdateMsg();")

            'ddlBillingFrom.Attributes.Add("onselected", "return DeleteMsg();")

            lblprodname.Text = ReturnValue("Select Product_Name from Product_Master where Product_Kid= '" & Session("ProductID").ToString() & "'and Product_IsDeleted='0' and Product_FinancialYearId ='" & Session("g_FinYear").ToString & "' and Product_CompanyId ='" & Session("g_Company").ToString & "'")
            lblProductCode.Text = ReturnValue("Select Product_Code from Product_Master where Product_Kid= '" & Session("ProductID").ToString() & "'and Product_IsDeleted='0' and Product_FinancialYearId ='" & Session("g_FinYear").ToString & "' and Product_CompanyId ='" & Session("g_Company").ToString & "'")
            lblModelNo.Text = ReturnValue("Select Product_ModelNo from Product_Master where Product_Kid= '" & Session("ProductID").ToString() & "'and Product_IsDeleted='0' and Product_FinancialYearId ='" & Session("g_FinYear").ToString & "' and Product_CompanyId ='" & Session("g_Company").ToString & "'")
            ' fill category herarchy in label
            FillFactoryCombo("SELECT SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Name + ' - ' + City_Master.City_Name AS [Factory Name] FROM SupplierFactoryDetails INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid WHERE (SupplierFactoryDetails.FactoryDetails_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (SupplierFactoryDetails.FactoryDetails_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "F")
            FillFactoryCombo("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlClient, "C")
            FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsAssc = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlOptionalAccessories, "A")
            FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsAssc = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlIncludedAccessories, "A")
            FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsSpareParts = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlSpareParts, "S")




            CancelClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
            ddlClient.Enabled = False

            CancelClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
            ddlIncludedAccessories.Enabled = False

            CancelClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
            ddlOptionalAccessories.Enabled = False

            CancelClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
            ddlSpareParts.Enabled = False

            Dim flag As String
            flag = ReturnValue("Select Product_BillingFromFlag from Product_Master where Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "' and Product_IsDeleted ='0'").ToString()

            If flag = "F" Then
                btnBillingSave.Enabled = False
                FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Code, SupplierFactoryDetails.FactoryDetails_Name + '-' + City_Master.City_Name AS FactoryDetails, SupplierFactoryDetails.FactoryDetails_Address FROM Product_Master INNER JOIN SupplierFactoryDetails ON Product_Master.Product_BillingFromId = SupplierFactoryDetails.FactoryDetails_Kid INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "F")
                enablebillingcontrol(False)
                lblBillingDetails.Visible = True
            ElseIf flag = "W" Then
                btnBillingSave.Enabled = False
                FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, StockingWarehouseInformation.swi_Kid, StockingWarehouseInformation.swi_Code, StockingWarehouseInformation.swi_whname + '-' + City_Master.City_Name AS WareHouse, StockingWarehouseInformation.swi_add FROM Product_Master INNER JOIN StockingWarehouseInformation ON Product_Master.Product_BillingFromId = StockingWarehouseInformation.swi_Kid INNER JOIN City_Master ON StockingWarehouseInformation.swi_city = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "W")
                enablebillingcontrol(False)
                lblBillingDetails.Visible = True
            Else
                lblBillingDetails.Visible = False
                enablebillingcontrol(True)
            End If

            If Session("AdminID") <> Nothing Then
                btnBillingDelete.Enabled = False
                btnBillingUpdate.Enabled = False
            Else
                btnBillingDelete.Visible = False
                btnBillingUpdate.Visible = False
                btnClientDelete.Visible = False
                btnClientUpdate.Visible = False
                btnIncludedAcceDelete.Visible = False
                btnIncludedAcceUpdate.Visible = False
                btnOptionAccDelete.Visible = False
                btnOptionAccUpdate.Visible = False
                btnSparePartsDelete.Visible = False
                btnSparePartsUpdate.Visible = False
            End If

            'ddlBillingFrom.Attributes.Add("onchange", "return AddNewConfirm(this,'F');")

            ddlBillingFrom.Attributes.Add("onchange", "return AddNewConfirm(this,'F','" & Session("Index").ToString() & "');")
            ddlClient.Attributes.Add("onchange", "return AddNewConfirm(this,'C','" & Session("Index").ToString() & "');")

            Menu1.Items(0).Selected = True
        End If
    End Sub

    Public Sub getexisting()
        Try
            Select Case Session("Index").ToString()
                Case "0"
                    Mtv.ActiveViewIndex = 0
                    FillFactoryCombo("SELECT SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Name + ' - ' + City_Master.City_Name AS [Factory Name] FROM SupplierFactoryDetails INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid WHERE (SupplierFactoryDetails.FactoryDetails_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (SupplierFactoryDetails.FactoryDetails_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "F")
                    'salescontacts()
                Case "1"
                    Mtv.ActiveViewIndex = 1
                    FillFactoryCombo("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlClient, "C")
                    CancelClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
                    ddlClient.Enabled = False
                    'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
                    Fill_Grid("SELECT Client_Details.ctd_kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS Client, Client_Details.ctd_productid, Client_Master.ctm_IsDeleted FROM Client_Details INNER JOIN Client_Master ON Client_Details.ctd_ctmId = Client_Master.ctm_kid INNER JOIN City_Master ON Client_Master.ctm_city = City_Master.City_Kid WHERE (Client_Details.ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = N'0') AND (Client_Details.ctd_IsDeleted = N'0')", gvClient)
                    If gvClient.Rows.Count = 0 Then
                        lblClientDetails.Visible = False
                    Else
                        lblClientDetails.Visible = True
                    End If
                    'Purchasecontacts()

                Case "2"
                    Mtv.ActiveViewIndex = 2
                    CancelClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
                    FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsAssc = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlOptionalAccessories, "A")
                    FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsAssc = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlIncludedAccessories, "A")
                    FillOptionalGrid()
                    FillIncludedGrid()
                    'Fill_Grid("SELECT OtherDetails_OptionalAccessories.ODTOA_kid, Product_Master.Product_name + ' - ' + Product_Master.Product_Code AS Name, OtherDetails_OptionalAccessories.ODTOA_productid, Product_Master.Product_IsDeleted FROM OtherDetails_OptionalAccessories INNER JOIN Product_Master ON OtherDetails_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_kid WHERE (OtherDetails_OptionalAccessories.ODTOA_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Product_Master.Product_IsDeleted = N'0') AND (OtherDetails_OptionalAccessories.ODTOA_IsDeleted = N'0')", gvOptionalAccesories)
                    'Technicalcontacts()
                    If gvOptionalAccesories.Rows.Count = 0 Then
                        lblOptDetails.Visible = False
                    Else
                        lblOptDetails.Visible = True
                    End If

                    If gvIncludedAccessories.Rows.Count = 0 Then
                        lblIncDetails.Visible = False
                    Else
                        lblIncDetails.Visible = True
                    End If

                Case "3"
                    Mtv.ActiveViewIndex = 3
                    FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsSpareParts = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlSpareParts, "S")
                    FillSparePartsGrid()
                    'Logisticscontacts()
                    If gvSpareParts.Rows.Count = 0 Then
                        lblSparePartsDetails.Visible = False
                    Else
                        lblSparePartsDetails.Visible = True
                    End If

                Case "4"
                    Response.Redirect("instore-services.aspx")
            End Select
        Catch ex As Exception

        End Try
    End Sub
    Public Sub enablebillingcontrol(ByVal action As Boolean)
        rblBillingFrom.Enabled = action
        ddlBillingFrom.Enabled = action
    End Sub
    Public Sub clearbillingcontrol()
        rblBillingFrom.ClearSelection()
        ddlBillingFrom.ClearSelection()
    End Sub

    Public Sub FillCategoryName(ByVal Catid As String)
        Try
            ' Fill category herarchy in label
            Dim dt As DataTable
            dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0'")
            Dim ParentId As String
            ParentId = dt.Rows(0).Item(3).ToString()
            If ParentId = "Root Category" Then
                Dim j As Integer
                Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
                For j = 0 To i
                    lblCat.Text += Category(i)
                    i = i - 1
                Next
                Exit Sub
            Else
                Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
                i = i + 1
                FillCategoryName(dt.Rows(0).Item(3).ToString())
            End If
            dt.Clear()
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub rblBillingFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblBillingFrom.SelectedIndexChanged
        Try

            If rblBillingFrom.SelectedItem.Value = "F" Then
                ddlBillingFrom.Attributes.Add("onchange", "return AddNewConfirm(this,'F','" & Session("Index").ToString() & "');")
                FillFactoryCombo("SELECT SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Name + ' -  ' + City_Master.City_Name AS [Factory Name] FROM SupplierFactoryDetails INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid WHERE (SupplierFactoryDetails.FactoryDetails_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (SupplierFactoryDetails.FactoryDetails_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "F")
            Else
                ddlBillingFrom.Attributes.Add("onchange", "return AddNewConfirm(this,'W','" & Session("Index").ToString() & "');")
                FillFactoryCombo("SELECT StockingWareHouseInformation.swi_Kid, StockingWareHouseInformation.swi_whname + ' -  ' + City_Master.City_Name AS [WareHouse Name] FROM StockingWareHouseInformation INNER JOIN City_Master ON StockingWareHouseInformation.swi_city = City_Master.City_Kid WHERE (StockingWareHouseInformation.swi_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (StockingWareHouseInformation.swi_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "W")
            End If
            'gvBillingFrom.DataSource = Nothing
            'gvBillingFrom.DataBind()
        Catch ex As Exception

        End Try

    End Sub
    Public Sub FillFactoryCombo(ByVal strQuery As String, ByVal ddl As DropDownList, ByVal flag As String)
        Try
            Dim SqlCon As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))  ' Declare connection Object
            Dim SqlAdapter As New SqlDataAdapter      ' Declare Dataadapter Object
            Dim SqlDataSet As New Data.DataSet        ' Declare Dataset Object
            Dim DT As New Data.DataTable              ' Declare Datatable Oblect
            Dim sqlCmd As New SqlCommand(strQuery, SqlCon) ' Declare Command Object
            Dim i As Integer

            SqlCon.Open()                                 ' Open Connection
            SqlAdapter.SelectCommand = sqlCmd             ' Set command to Adapter
            SqlAdapter.Fill(SqlDataSet, "TableName")      ' Links Adapter with DataSet
            DT = SqlDataSet.Tables("TableName")           ' Get datatable from dataset
            sqlCmd.ExecuteNonQuery()                      ' Executes Command

            If flag = "F" Then
                ddl.Items.Clear()                             ' Clear the Items of DropDownList  
                ddl.Items.Insert(0, New ListItem("Select FactoryName - City", "Select"))     ' Insert Blank Row at index 0  
            ElseIf flag = "W" Then
                ddl.Items.Clear()                             ' Clear the Items of DropDownList  
                ddl.Items.Insert(0, New ListItem("Select WareHouseName - City", "Select"))     ' Insert Blank Row at index 0  
            ElseIf flag = "C" Then
                ddl.Items.Clear()                             ' Clear the Items of DropDownList  
                ddl.Items.Insert(0, New ListItem("Select ClientName - City", "Select"))     ' Insert Blank Row at index 0  
            ElseIf flag = "A" Then
                ddl.Items.Clear()                             ' Clear the Items of DropDownList  
                ddl.Items.Insert(0, New ListItem("Select Asseccory Name - Code - ModelNo", "Select"))     ' Insert Blank Row at index 0  
            ElseIf flag = "S" Then
                ddl.Items.Clear()                             ' Clear the Items of DropDownList  
                ddl.Items.Insert(0, New ListItem("Select SparePart Name - Code - ModelNo", "Select"))     ' Insert Blank Row at index 0              ElseIf flag = "S" Then
            End If

            For i = 0 To DT.Rows.Count - 1
                ddl.Items.Insert(i + 1, New ListItem(DT.Rows(i).Item(1), DT.Rows(i).Item(0)))  '' Add Items(code,Exact Value) into DropDownList
            Next i
            ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(""))     '' Assigning 0 th Index to DropDownList

            If flag = "F" Then
                ddl.Items.Add(New ListItem("Add New Factory", "Add New"))
            ElseIf flag = "W" Then
                ddl.Items.Add(New ListItem("Add New WareHouse", "Add New"))
            ElseIf flag = "C" Then
                ddl.Items.Add(New ListItem("Add New Client", "Add New"))
            End If

            SqlCon.Close()                                '' Close Connection Object
            SqlCon.Dispose()                              '' Dispose Connection Oblect
            sqlCmd.Dispose()                              '' Dispose command object
            SqlAdapter.Dispose()                          '' Dispose adapter Object
            SqlDataSet.Dispose()                          '' Dispose dataset Object
            DT.Dispose()                                  '' Dispose datatable Object
        Catch ex As Exception
            CreateMessageAlert(ddl.Page, ex.Message, "StrKeyVal")     'Display the Error Message
            ddl = Nothing
        End Try
    End Sub
    Protected Sub ddlBillingFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'If ddlBillingFrom.SelectedIndex > 0 Then
        '    If rblBillingFrom.SelectedItem.Value = "F" Then
        '        gvBillingFrom.Columns.Clear()
        '        fillfactorygrid("SELECT SupplierFactoryDetails.*, City_Master.City_Name, SupplierFactoryDetails.FactoryDetails_IsDeleted AS Expr2, City_Master.City_IsDeleted, State_Master.State_Name, Country_Master.Country_Name FROM Country_Master INNER JOIN State_Master ON Country_Master.Country_Kid = State_Master.State_CountryId INNER JOIN SupplierFactoryDetails INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid ON State_Master.State_Kid = City_Master.City_StateId WHERE (SupplierFactoryDetails.FactoryDetails_Kid = '" & RemoveLiterals(ddlBillingFrom.SelectedItem.Value.ToString.Trim) & "') AND (SupplierFactoryDetails.FactoryDetails_IsDeleted = N'0') AND (City_Master.City_IsDeleted = N'0')", gvBillingFrom)
        '    Else
        '        gvBillingFrom.Columns.Clear()
        '        fillwarehousegrid("SELECT StockingWareHouseInformation.*, City_Master.City_Name, StockingWareHouseInformation.swi_IsDeleted AS Expr2, City_Master.City_IsDeleted, State_Master.State_Name, Country_Master.Country_Name FROM Country_Master INNER JOIN State_Master ON Country_Master.Country_Kid = State_Master.State_CountryId INNER JOIN StockingWareHouseInformation INNER JOIN City_Master ON StockingWareHouseInformation.swi_city = City_Master.City_Kid ON State_Master.State_Kid = City_Master.City_StateId WHERE (StockingWareHouseInformation.swi_Kid = '" & RemoveLiterals(ddlBillingFrom.SelectedItem.Value.ToString.Trim) & "') AND (StockingWareHouseInformation.swi_IsDeleted = N'0') AND (City_Master.City_IsDeleted = N'0')", gvBillingFrom)
        '    End If
        'Else
        '    gvBillingFrom.DataSource = Nothing
        '    gvBillingFrom.DataBind()
        'End If
    End Sub
    'Public Sub fillfactorygrid(ByVal StrQry As String, ByVal gv As GridView)
    '    ' used to fill grid acounrding to StrQry Parameter
    '    Try
    '        Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
    '        Con.Open()                                             'Open the Connection
    '        Dim cmd As New SqlCommand(StrQry, 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
    '        Dim b, b1, b2, b3, b4, b5, b6 As New BoundField

    '        b.HeaderText = "Factory Name"
    '        b.DataField = "FactoryDetails_Name"
    '        gv.Columns.Add(b)
    '        b1.HeaderText = "Country"
    '        b1.DataField = "Country_Name"
    '        gv.Columns.Add(b1)

    '        b2.HeaderText = "State"
    '        b2.DataField = "State_Name"
    '        gv.Columns.Add(b2)

    '        b3.HeaderText = "City"
    '        b3.DataField = "City_Name"
    '        gv.Columns.Add(b3)

    '        b4.HeaderText = "Address"
    '        b4.DataField = "FactoryDetails_Address"
    '        gv.Columns.Add(b4)

    '        b5.HeaderText = "Phone No"
    '        b5.DataField = "FactoryDetails_PhoneNo"
    '        gv.Columns.Add(b5)

    '        b6.HeaderText = "Email"
    '        b6.DataField = "FactoryDetails_EmailID"
    '        gv.Columns.Add(b6)


    '        gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
    '        gv.DataBind()


    '        If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close
    '        Con = Nothing                                        ' Set Connnection Object as nothing       
    '        cmd = Nothing                                        ' Set Command Object as nothing
    '        dr = Nothing                                         ' Set DataReader Object as nothing
    '        'Dim i As Integer
    '        'For i = 0 To gv.Rows.Count - 1
    '        '    gv.Rows(i).Cells(4).Text = gv.Rows(i).Cells(3).Text & "  " & gv.Rows(i).Cells(4).Text
    '        '    If gv.Rows(i).Cells(7).Text.ToString <> "&nbsp;" Then
    '        '        gv.Rows(i).Cells(8).Text = gv.Rows(i).Cells(7).Text & " - " & gv.Rows(i).Cells(8).Text
    '        '    End If
    '        'Next
    '    Catch ex As Exception
    '        ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
    '    End Try
    'End Sub
    'Public Sub fillwarehousegrid(ByVal StrQry As String, ByVal gv As GridView)
    '    ' used to fill grid acounrding to StrQry Parameter
    '    Try
    '        Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
    '        Con.Open()                                             'Open the Connection
    '        Dim cmd As New SqlCommand(StrQry, 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
    '        Dim b, b1, b2, b3, b4, b5, b6 As New BoundField
    '        b.HeaderText = "WareHouse Name"
    '        b.DataField = "swi_whname"
    '        gv.Columns.Add(b)
    '        b1.HeaderText = "Country"
    '        b1.DataField = "Country_Name"
    '        gv.Columns.Add(b1)

    '        b2.HeaderText = "State"
    '        b2.DataField = "State_Name"
    '        gv.Columns.Add(b2)

    '        b3.HeaderText = "City"
    '        b3.DataField = "City_Name"
    '        gv.Columns.Add(b3)

    '        b4.HeaderText = "Address"
    '        b4.DataField = "swi_add"
    '        gv.Columns.Add(b4)

    '        b5.HeaderText = "Phone No"
    '        b5.DataField = "swi_phno3"
    '        gv.Columns.Add(b5)

    '        b6.HeaderText = "Email"
    '        b6.DataField = "swi_eid"
    '        gv.Columns.Add(b6)

    '        gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
    '        gv.DataBind()

    '        If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close
    '        Con = Nothing                                        ' Set Connnection Object as nothing       
    '        cmd = Nothing                                        ' Set Command Object as nothing
    '        dr = Nothing                                         ' Set DataReader Object as nothing
    '        'Dim i As Integer
    '        'For i = 0 To gv.Rows.Count - 1
    '        '    gv.Rows(i).Cells(4).Text = gv.Rows(i).Cells(3).Text & "  " & gv.Rows(i).Cells(4).Text
    '        '    If gv.Rows(i).Cells(7).Text.ToString <> "&nbsp;" Then
    '        '        gv.Rows(i).Cells(8).Text = gv.Rows(i).Cells(7).Text & " - " & gv.Rows(i).Cells(8).Text
    '        '    End If
    '        'Next
    '    Catch ex As Exception
    '        ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
    '    End Try
    'End Sub

    Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick

        Try
            'Dim i As Integer
            'i = e.Item.Value
            Select Case Menu1.SelectedItem.Value
                Case "0"
                    Mtv.ActiveViewIndex = 0
                    FillFactoryCombo("SELECT SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Name + ' - ' + City_Master.City_Name AS [Factory Name] FROM SupplierFactoryDetails INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid WHERE (SupplierFactoryDetails.FactoryDetails_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (SupplierFactoryDetails.FactoryDetails_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "F")
                    'salescontacts()
                    Session("Index") = Mtv.ActiveViewIndex.ToString()
                Case "1"
                    Mtv.ActiveViewIndex = 1
                    FillFactoryCombo("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlClient, "C")
                    CancelClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
                    ddlClient.Enabled = False
                    'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
                    Fill_Grid("SELECT Client_Details.ctd_kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS Client, Client_Details.ctd_productid, Client_Master.ctm_IsDeleted FROM Client_Details INNER JOIN Client_Master ON Client_Details.ctd_ctmId = Client_Master.ctm_kid INNER JOIN City_Master ON Client_Master.ctm_city = City_Master.City_Kid WHERE (Client_Details.ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = N'0') AND (Client_Details.ctd_IsDeleted = N'0')", gvClient)
                    If gvClient.Rows.Count = 0 Then
                        lblClientDetails.Visible = False
                    Else
                        lblClientDetails.Visible = True
                    End If
                    'Purchasecontacts()
                    Session("Index") = Mtv.ActiveViewIndex.ToString()
                Case "2"
                    Mtv.ActiveViewIndex = 2
                    CancelClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
                    FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsAssc = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlOptionalAccessories, "A")
                    FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsAssc = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlIncludedAccessories, "A")
                    FillOptionalGrid()
                    FillIncludedGrid()
                    'Fill_Grid("SELECT OtherDetails_OptionalAccessories.ODTOA_kid, Product_Master.Product_name + ' - ' + Product_Master.Product_Code AS Name, OtherDetails_OptionalAccessories.ODTOA_productid, Product_Master.Product_IsDeleted FROM OtherDetails_OptionalAccessories INNER JOIN Product_Master ON OtherDetails_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_kid WHERE (OtherDetails_OptionalAccessories.ODTOA_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Product_Master.Product_IsDeleted = N'0') AND (OtherDetails_OptionalAccessories.ODTOA_IsDeleted = N'0')", gvOptionalAccesories)
                    'Technicalcontacts()
                    If gvOptionalAccesories.Rows.Count = 0 Then
                        lblOptDetails.Visible = False
                    Else
                        lblOptDetails.Visible = True
                    End If
                    Session("Index") = Mtv.ActiveViewIndex.ToString()
                    If gvIncludedAccessories.Rows.Count = 0 Then
                        lblIncDetails.Visible = False
                    Else
                        lblIncDetails.Visible = True
                    End If

                Case "3"
                    Mtv.ActiveViewIndex = 3
                    FillFactoryCombo("SELECT Product_Master.Product_Kid, Product_Master.Product_Name + ' - ' + Product_Master.Product_Code + ' - ' + isnull(Product_Master.Product_ModelNo,'') AS [Asseccory Name] FROM Product_Master WHERE (Product_Master.Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (Product_Master.Product_IsSpareParts = 'Y') AND (Product_Master.Product_IsDeleted = '0') AND (Product_Master.Product_Kid <> '" & Session("ProductID").ToString.Trim & "')", ddlSpareParts, "S")
                    FillSparePartsGrid()
                    Session("Index") = Mtv.ActiveViewIndex.ToString()
                    'Logisticscontacts()
                    If gvSpareParts.Rows.Count = 0 Then
                        lblSparePartsDetails.Visible = False
                    Else
                        lblSparePartsDetails.Visible = True
                    End If

                Case "4"
                    Response.Redirect("instore-services.aspx")
            End Select
        Catch ex As Exception

        End Try

    End Sub

    Public Sub FillSparePartsGrid()
        Try

            Dim OptDt As DataTable
            Dim dt1 As DataTable = getTable("Select ODTSP_SparePartId From OtherDetail_SpareParts where ODTSP_ProductId ='" & RemoveLiterals(Session("ProductID").ToString.Trim) & "'and ODTSP_IsDeleted = '0'", "SpareParts")
            Dim i As Integer
            Dim Kid As String
            Dim KidBool As Boolean = True
            'ddl.ClearSelection()
            For i = 0 To dt1.Rows.Count - 1
                If KidBool = True Then
                    Kid = "'" & dt1.Rows(i).Item(0).ToString & "'"
                    KidBool = False
                Else
                    Kid &= ","
                    Kid &= "'" & dt1.Rows(i).Item(0).ToString & "'"

                End If

            Next
            dt1.Clear()
            dt1.Dispose()

            'dt.Clear()
            'OptDt1 = ReturnDataTable("Select * From OtherDetail_OptionalAccessories where ODTOA_ProductId ='" & RemoveLiterals(Session("ProductID").ToString.Trim) & "'and ODTOA_IsDeleted = '0'")

            'If OptDt1.Rows.Count > 0 Then
            'Dim i As Integer
            'For i = 0 To OptDt1.Rows.Count - 1
            OptDt = ReturnDataTable("SELECT Product_Master.*, OtherDetail_SpareParts.ODTSP_kid, OtherDetail_SpareParts.ODTSP_SparePartId FROM Product_Master INNER JOIN OtherDetail_SpareParts ON Product_Master.Product_Kid = OtherDetail_SpareParts.ODTSP_SparePartId WHERE Product_Master.Product_Kid IN (" & Kid & ")AND OtherDetail_SpareParts.ODTSP_IsDeleted = '0' AND Product_Master.Product_IsDeleted = '0'")
            'OptDt = ReturnDataTable("Select * From Product_Master where Product_Kid in ( " & Kid & ") and Product_IsDeleted ='0'")
            gvSpareParts.DataSource = OptDt
            gvSpareParts.DataBind()

            Dim count As Integer
            For count = 0 To gvSpareParts.Rows.Count - 1
                If gvSpareParts.Rows(count).Cells(4).Text = "Y" Then
                    gvSpareParts.Rows(count).Cells(4).Text = "Yes"
                ElseIf gvSpareParts.Rows(count).Cells(4).Text = "N" Then
                    gvSpareParts.Rows(count).Cells(4).Text = "No"
                End If
            Next
            OptDt.Clear()
            OptDt.Dispose()
            'Next
            'Else

            'End If
            'Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            'Con.Open()                                             'Open the Connection
            'Dim cmd As New SqlCommand(StrQry, 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
            'gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
            'gv.DataBind()
            'If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close
            'Con = Nothing                                        ' Set Connnection Object as nothing       
            'cmd = Nothing                                        ' Set Command Object as nothing
            'dr = Nothing                                         ' Set DataReader Object as nothing
        Catch ex As Exception
            ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Public Sub FillIncludedGrid()
        Try

            Dim OptDt As DataTable
            Dim dt1 As DataTable = getTable("Select ODTIA_InAccessId From OtherDetail_IncludedAccessories where ODTIA_ProductId ='" & RemoveLiterals(Session("ProductID").ToString.Trim) & "'and ODTIA_IsDeleted = '0'", "Included")
            Dim i As Integer
            Dim Kid As String
            Dim KidBool As Boolean = True
            'ddl.ClearSelection()
            For i = 0 To dt1.Rows.Count - 1
                If KidBool = True Then
                    Kid = "'" & dt1.Rows(i).Item(0).ToString & "'"
                    KidBool = False
                Else
                    Kid &= ","
                    Kid &= "'" & dt1.Rows(i).Item(0).ToString & "'"

                End If

            Next
            dt1.Clear()
            dt1.Dispose()
            'dt.Clear()
            'OptDt1 = ReturnDataTable("Select * From OtherDetail_OptionalAccessories where ODTOA_ProductId ='" & RemoveLiterals(Session("ProductID").ToString.Trim) & "'and ODTOA_IsDeleted = '0'")

            'If OptDt1.Rows.Count > 0 Then
            'Dim i As Integer
            'For i = 0 To OptDt1.Rows.Count - 1
            OptDt = ReturnDataTable("SELECT Product_Master.*, OtherDetail_IncludedAccessories.ODTIA_kid, OtherDetail_IncludedAccessories.ODTIA_InAccessId FROM Product_Master INNER JOIN OtherDetail_IncludedAccessories ON Product_Master.Product_Kid = OtherDetail_IncludedAccessories.ODTIA_InAccessId WHERE Product_Master.Product_Kid IN (" & Kid & ")AND OtherDetail_IncludedAccessories.ODTIA_IsDeleted = '0' AND Product_Master.Product_IsDeleted = '0'")
            'OptDt = ReturnDataTable("Select * From Product_Master where Product_Kid in ( " & Kid & ") and Product_IsDeleted ='0'")
            gvIncludedAccessories.DataSource = OptDt
            gvIncludedAccessories.DataBind()
            OptDt.Clear()
            OptDt.Dispose()
            'Next
            'Else

            'End If
            'Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            'Con.Open()                                             'Open the Connection
            'Dim cmd As New SqlCommand(StrQry, 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
            'gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
            'gv.DataBind()
            'If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close
            'Con = Nothing                                        ' Set Connnection Object as nothing       
            'cmd = Nothing                                        ' Set Command Object as nothing
            'dr = Nothing                                         ' Set DataReader Object as nothing
        Catch ex As Exception
            ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    Public Sub FillOptionalGrid()
        Try

            Dim OptDt As DataTable
            Dim dt1 As DataTable = getTable("Select ODTOA_OAccessId From OtherDetail_OptionalAccessories where ODTOA_ProductId ='" & RemoveLiterals(Session("ProductID").ToString.Trim) & "'and ODTOA_IsDeleted = '0'", "Optional")
            Dim i As Integer
            Dim Kid As String
            Dim KidBool As Boolean = True
            'ddl.ClearSelection()
            For i = 0 To dt1.Rows.Count - 1
                If KidBool = True Then
                    Kid = "'" & dt1.Rows(i).Item(0).ToString & "'"
                    KidBool = False
                Else
                    Kid &= ","
                    Kid &= "'" & dt1.Rows(i).Item(0).ToString & "'"

                End If

            Next
            dt1.Clear()
            dt1.Dispose()
            'dt.Clear()
            'OptDt1 = ReturnDataTable("Select * From OtherDetail_OptionalAccessories where ODTOA_ProductId ='" & RemoveLiterals(Session("ProductID").ToString.Trim) & "'and ODTOA_IsDeleted = '0'")

            'If OptDt1.Rows.Count > 0 Then
            'Dim i As Integer
            'For i = 0 To OptDt1.Rows.Count - 1
            OptDt = ReturnDataTable("SELECT Product_Master.*, OtherDetail_OptionalAccessories.ODTOA_kid, OtherDetail_OptionalAccessories.ODTOA_OAccessId FROM Product_Master INNER JOIN OtherDetail_OptionalAccessories ON Product_Master.Product_Kid = OtherDetail_OptionalAccessories.ODTOA_OAccessId WHERE Product_Master.Product_Kid IN (" & Kid & ")AND OtherDetail_OptionalAccessories.ODTOA_IsDeleted = '0' AND Product_Master.Product_IsDeleted = '0'")
            'OptDt = ReturnDataTable("Select * From Product_Master where Product_Kid in ( " & Kid & ") and Product_IsDeleted ='0'")
            gvOptionalAccesories.DataSource = OptDt
            gvOptionalAccesories.DataBind()
            'Next
            'Else

            OptDt.Clear()
            OptDt.Dispose()
            'End If
            'Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            'Con.Open()                                             'Open the Connection
            'Dim cmd As New SqlCommand(StrQry, 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
            'gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
            'gv.DataBind()
            'If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close
            'Con = Nothing                                        ' Set Connnection Object as nothing       
            'cmd = Nothing                                        ' Set Command Object as nothing
            'dr = Nothing                                         ' Set DataReader Object as nothing
        Catch ex As Exception
            ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    Protected Sub btnBillingSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBillingSave.Click
        Try

            If ddlBillingFrom.SelectedIndex > 0 Then
                btnBillingSave.Enabled = False
                ExecuteQuery("Update product_master set Product_BillingFromFlag='" & rblBillingFrom.SelectedItem.Value.Trim & "',Product_BillingFromId='" & RemoveLiterals(ddlBillingFrom.SelectedItem.Value.Trim) & "' where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'  ")
                showmessage("Record Saved Successfully", lblBillingMsg)
                If rblBillingFrom.SelectedValue = "F" Then
                    FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Code, SupplierFactoryDetails.FactoryDetails_Name + '-' + City_Master.City_Name AS FactoryDetails, SupplierFactoryDetails.FactoryDetails_Address FROM Product_Master INNER JOIN SupplierFactoryDetails ON Product_Master.Product_BillingFromId = SupplierFactoryDetails.FactoryDetails_Kid INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "F")
                ElseIf rblBillingFrom.SelectedValue = "W" Then
                    FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, StockingWarehouseInformation.swi_Kid, StockingWarehouseInformation.swi_Code, StockingWarehouseInformation.swi_whname + '-' + City_Master.City_Name AS WareHouse, StockingWarehouseInformation.swi_add FROM Product_Master INNER JOIN StockingWarehouseInformation ON Product_Master.Product_BillingFromId = StockingWarehouseInformation.swi_Kid INNER JOIN City_Master ON StockingWarehouseInformation.swi_city = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "W")
                End If
                enablebillingcontrol(False)
                clearbillingcontrol()
                lblBillingDetails.Visible = True
                CreateMessageAlert(Me, "Record Saved Successfully.", "StrKeyVal")
            Else
                lblerror.Text = "Please select billing from location."
            End If

            

        Catch ex As Exception

        End Try
    End Sub
    Public Sub FillGrid(ByVal StrQry As String, ByVal gv As GridView, ByVal flag As String)
        Try

            Dim key(0) As String
            Dim sel As New CommandField
            sel.HeaderText = "Select"
            sel.ShowSelectButton = True
            Dim b1, b2, b3 As New BoundField
            b1.HeaderText = "From"
            b1.DataField = "Product_BillingFromFlag"

            b2.HeaderText = "Name - City"
            b3.HeaderText = "Address"

            If flag = "F" Then
                key(0) = "FactoryDetails_Kid"
                b2.DataField = "FactoryDetails"
                b3.DataField = "FactoryDetails_Address"
                gv.DataKeyNames = key
            ElseIf flag = "W" Then
                key(0) = "swi_Kid"
                b2.DataField = "WareHouse"
                b3.DataField = "swi_add"
                gv.DataKeyNames = key
            End If
            gv.Columns.Clear()
            gv.Columns.Add(sel)
            gv.Columns.Add(b1)
            gv.Columns.Add(b2)
            gv.Columns.Add(b3)
            Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            Con.Open()                                             'Open the Connection
            Dim cmd As New SqlCommand(StrQry, 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
            gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
            gv.DataBind()
            If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close

            Dim i As Integer
            For i = 0 To gv.Rows.Count - 1
                If gv.Rows(i).Cells(1).Text = "F" Then
                    gv.Rows(i).Cells(1).Text = "Factory"
                ElseIf gv.Rows(i).Cells(1).Text = "W" Then
                    gv.Rows(i).Cells(1).Text = "WareHouse"
                End If
            Next

            Con = Nothing                                        ' Set Connnection Object as nothing       
            cmd = Nothing                                        ' Set Command Object as nothing
            dr = Nothing                                         ' Set DataReader Object as nothing
        Catch ex As Exception
            ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub gvBillingFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvBillingFrom.SelectedIndexChanged

        Try
            'Dim id As String
            'id = gvBillingFrom.SelectedDataKey.Value

            If Session("AdminID") <> Nothing Then
                btnBillingDelete.Enabled = True
                btnBillingUpdate.Enabled = True
                enablebillingcontrol(True)
            Else
                enablebillingcontrol(False)
            End If
            Dim flag As String
            flag = ReturnValue("select product_billingfromflag from product_master where product_billingfromid = '" & RemoveLiterals(gvBillingFrom.SelectedDataKey.Value.ToString.Trim) & "'and product_isdeleted = '0'")
            If flag = "F" Then
                rblBillingFrom.Items.FindByValue(flag).Selected = True
                FillFactoryCombo("SELECT SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Name + ' -  ' + City_Master.City_Name AS [Factory Name] FROM SupplierFactoryDetails INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid WHERE (SupplierFactoryDetails.FactoryDetails_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (SupplierFactoryDetails.FactoryDetails_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "F")
                ddlBillingFrom.Items.FindByValue(RemoveLiterals(gvBillingFrom.SelectedDataKey.Value)).Selected = True
            ElseIf flag = "W" Then
                rblBillingFrom.Items.FindByValue(flag).Selected = True
                FillFactoryCombo("SELECT StockingWareHouseInformation.swi_Kid, StockingWareHouseInformation.swi_whname + ' -  ' + City_Master.City_Name AS [WareHouse Name] FROM StockingWareHouseInformation INNER JOIN City_Master ON StockingWareHouseInformation.swi_city = City_Master.City_Kid WHERE (StockingWareHouseInformation.swi_SupplierId = '" & Session("SupplierID").ToString.Trim & "') AND (StockingWareHouseInformation.swi_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", ddlBillingFrom, "W")
                ddlBillingFrom.Items.FindByValue(RemoveLiterals(gvBillingFrom.SelectedDataKey.Value)).Selected = True
            Else
                rblBillingFrom.ClearSelection()
                ddlBillingFrom.ClearSelection()
            End If
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnBillingUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBillingUpdate.Click
        Try

            If ddlBillingFrom.SelectedIndex > 0 Then
                btnBillingSave.Enabled = False
                ExecuteQuery("Update product_master set Product_BillingFromFlag='" & rblBillingFrom.SelectedItem.Value.Trim & "',Product_BillingFromId='" & RemoveLiterals(ddlBillingFrom.SelectedItem.Value.Trim) & "' where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'  ")
                showmessage("Record Updated Successfully", lblBillingMsg)
                If rblBillingFrom.SelectedValue = "F" Then
                    FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Code, SupplierFactoryDetails.FactoryDetails_Name + '-' + City_Master.City_Name AS FactoryDetails, SupplierFactoryDetails.FactoryDetails_Address FROM Product_Master INNER JOIN SupplierFactoryDetails ON Product_Master.Product_BillingFromId = SupplierFactoryDetails.FactoryDetails_Kid INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "F")
                ElseIf rblBillingFrom.SelectedValue = "W" Then
                    FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, StockingWarehouseInformation.swi_Kid, StockingWarehouseInformation.swi_Code, StockingWarehouseInformation.swi_whname + '-' + City_Master.City_Name AS WareHouse, StockingWarehouseInformation.swi_add FROM Product_Master INNER JOIN StockingWarehouseInformation ON Product_Master.Product_BillingFromId = StockingWarehouseInformation.swi_Kid INNER JOIN City_Master ON StockingWarehouseInformation.swi_city = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "W")
                End If
                enablebillingcontrol(False)
                clearbillingcontrol()
                btnBillingUpdate.Enabled = False
                btnBillingDelete.Enabled = False
                CreateMessageAlert(Me, "Record Updated Successfully.", "StrKeyVal")
            Else
                lblerror.Text = "Please select billing from location."
            End If

            

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnBillingDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBillingDelete.Click
        Try

            If ddlBillingFrom.SelectedIndex > 0 Then
                btnBillingSave.Enabled = False
                ExecuteQuery("Update product_master set Product_BillingFromFlag='',Product_BillingFromId='' where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'  ")
                showmessage("Record Deleted Successfully", lblBillingMsg)
                enablebillingcontrol(False)
                clearbillingcontrol()
                CreateMessageAlert(Me, "Record Deleted Successfully.", "StrKeyVal")
            Else
                lblerror.Text = "Please select billing from location."
            End If

            gvBillingFrom.DataSource = Nothing
            gvBillingFrom.DataBind()
            'If rblBillingFrom.SelectedValue = "F" Then
            '    FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, SupplierFactoryDetails.FactoryDetails_Kid, SupplierFactoryDetails.FactoryDetails_Code, SupplierFactoryDetails.FactoryDetails_Name + '-' + City_Master.City_Name AS FactoryDetails, SupplierFactoryDetails.FactoryDetails_Address FROM Product_Master INNER JOIN SupplierFactoryDetails ON Product_Master.Product_BillingFromId = SupplierFactoryDetails.FactoryDetails_Kid INNER JOIN City_Master ON SupplierFactoryDetails.FactoryDetails_LocationId = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "F")
            'ElseIf rblBillingFrom.SelectedValue = "W" Then
            '    FillGrid("SELECT Product_Master.Product_Kid, Product_Master.Product_BillingFromFlag, StockingWarehouseInformation.swi_Kid, StockingWarehouseInformation.swi_Code, StockingWarehouseInformation.swi_whname + '-' + City_Master.City_Name AS WareHouse, StockingWarehouseInformation.swi_add FROM Product_Master INNER JOIN StockingWarehouseInformation ON Product_Master.Product_BillingFromId = StockingWarehouseInformation.swi_Kid INNER JOIN City_Master ON StockingWarehouseInformation.swi_city = City_Master.City_Kid where product_kid='" & Session("ProductID").ToString.Trim & "' and Product_SupplierId = '" & Session("SupplierID").ToString.Trim & "' and Product_IsDeleted = '0'", gvBillingFrom, "W")
            'End If
            btnBillingSave.Enabled = True
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnClientAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClientAdd.Click

        Try

            ddlClient.Enabled = True
            AddClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnClientSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClientSave.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM Client_Details WHERE (ctd_IsDeleted = 0) AND (ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ctd_ctmId = '" & RemoveLiterals(ddlClient.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Client is Already Present.", lblClientField)
                'ddlClient.ClearSelection()
                ScriptManager1.SetFocus(ddlClient) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ctd_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(NewPrimaryKey(Me))
            sqlcoll.Add("@ctd_Code", Data.SqlDbType.NVarChar, 25).Value = GenerateClientCode().ToString
            sqlcoll.Add("@ctd_productid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ctd_ctmId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlClient.SelectedItem.Value)
            sqlcoll.Add("@ctd_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ctd_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ctd_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Insert"
            ExecQuery("Client_details_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Saved Successfully.", lblClientField)
            ddlClient.ClearSelection()
            ddlClient.Enabled = False
            Fill_Grid("SELECT Client_Details.ctd_kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS Client, Client_Details.ctd_productid, Client_Master.ctm_IsDeleted FROM Client_Details INNER JOIN Client_Master ON Client_Details.ctd_ctmId = Client_Master.ctm_kid INNER JOIN City_Master ON Client_Master.ctm_city = City_Master.City_Kid WHERE (Client_Details.ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = N'0') AND (Client_Details.ctd_IsDeleted = N'0')", gvClient)
            lblClientDetails.Visible = True
            CreateMessageAlert(Me, "Record Saved Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            SaveClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)

        Catch ex As Exception

        End Try
    End Sub
    Protected Function GenerateClientCode() As String
        ' generate supplier factory details code and kid
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "CDT" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from Client_Details where ctd_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Function

    
    Protected Sub btnClientCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClientCancel.Click
        Try

            CancelClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
            ddlClient.Enabled = False
            ddlClient.ClearSelection()
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub gvClient_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvClient.SelectedIndexChanged
        Try

            Dim kid As String
            kid = ReturnValue("Select ctd_ctmId from Client_Details where ctd_kid ='" & RemoveLiterals(gvClient.SelectedDataKey.Value.ToString.Trim) & "'and ctd_IsDeleted = '0'")
            ddlClient.ClearSelection()
            ddlClient.Items.FindByValue(kid.ToString.Trim).Selected = True
            GridClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
            ddlClient.Enabled = True
            If Session("AdminID") <> Nothing Then
                ddlClient.Enabled = True
            Else
                ddlClient.Enabled = False
            End If
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnClientUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClientUpdate.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM Client_Details WHERE (ctd_IsDeleted = 0) AND (ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ctd_ctmId = '" & RemoveLiterals(ddlClient.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Client is Already Present.", lblClientField)
                ' ddlClient.ClearSelection()
                ScriptManager1.SetFocus(ddlClient) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ctd_Kid", Data.SqlDbType.NVarChar, 10).Value = (gvClient.SelectedDataKey.Value.ToString.Trim)
            sqlcoll.Add("@ctd_Code", Data.SqlDbType.NVarChar, 25).Value = ReturnValue("Select ctd_code from Client_Details where ctd_kid = '" & RemoveLiterals(gvClient.SelectedDataKey.Value.ToString.Trim) & "'and ctd_IsDeleted ='0'")
            sqlcoll.Add("@ctd_productid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ctd_ctmId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlClient.SelectedItem.Value)
            sqlcoll.Add("@ctd_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ctd_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ctd_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Update"
            ExecQuery("Client_details_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Updated Successfully.", lblClientField)
            ddlClient.ClearSelection()
            ddlClient.Enabled = False
            Fill_Grid("SELECT Client_Details.ctd_kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS Client, Client_Details.ctd_productid, Client_Master.ctm_IsDeleted FROM Client_Details INNER JOIN Client_Master ON Client_Details.ctd_ctmId = Client_Master.ctm_kid INNER JOIN City_Master ON Client_Master.ctm_city = City_Master.City_Kid WHERE (Client_Details.ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = N'0') AND (Client_Details.ctd_IsDeleted = N'0')", gvClient)
            lblClientDetails.Visible = False
            CreateMessageAlert(Me, "Record Updated Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            EditClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnClientDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClientDelete.Click

        Try

            If ddlClient.SelectedIndex > 0 Then
                ExecuteQuery("Update Client_Details set ctd_isdeleted ='1' where ctd_kid = '" & RemoveLiterals(gvClient.SelectedDataKey.Value.ToString.Trim) & "'")
                showmessage("Record Deleted Successfully.", lblClientField)
                ddlClient.ClearSelection()
                ddlClient.Enabled = False
                DeleteClick(Me, btnClientAdd, btnClientSave, btnClientUpdate, btnClientDelete, btnClientCancel, btnClientSearch)
                Fill_Grid("SELECT Client_Details.ctd_kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS Client, Client_Details.ctd_productid, Client_Master.ctm_IsDeleted FROM Client_Details INNER JOIN Client_Master ON Client_Details.ctd_ctmId = Client_Master.ctm_kid INNER JOIN City_Master ON Client_Master.ctm_city = City_Master.City_Kid WHERE (Client_Details.ctd_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = N'0') AND (Client_Details.ctd_IsDeleted = N'0')", gvClient)
                If gvClient.Rows.Count = 0 Then
                    lblClientDetails.Visible = False
                Else
                    lblClientDetails.Visible = True
                End If
                CreateMessageAlert(Me, "Record Deleted Successfully.", "StrKeyVal")
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnOptionAccAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOptionAccAdd.Click

        Try
            ddlOptionalAccessories.Enabled = True
            AddClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnIncludedAcceAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncludedAcceAdd.Click
        Try

            ddlIncludedAccessories.Enabled = True
            AddClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnOptionAccSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOptionAccSave.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM OtherDetail_OptionalAccessories WHERE (ODTOA_IsDeleted = '0') AND (ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ODTOA_OAccessId = '" & RemoveLiterals(ddlOptionalAccessories.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Optional Accessory is Already Present.", lblOptionalMsg)
                'ddlOptionalAccessories.ClearSelection()
                ScriptManager1.SetFocus(ddlOptionalAccessories) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ODTOA_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(NewPrimaryKey(Me))
            sqlcoll.Add("@ODTOA_Code", Data.SqlDbType.NVarChar, 25).Value = GenerateOptionalAcceCode().ToString
            sqlcoll.Add("@ODTOA_ProductId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ODTOA_OAccessId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlOptionalAccessories.SelectedItem.Value)
            sqlcoll.Add("@ODTOA_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ODTOA_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ODTOA_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Insert"
            ExecQuery("OtherDetail_OptionalAccessories_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Saved Successfully.", lblOptionalMsg)
            ddlOptionalAccessories.ClearSelection()
            ddlOptionalAccessories.Enabled = False
            FillOptionalGrid()
            lblOptDetails.Visible = True
            'Fill_Grid("SELECT OtherDetail_OptionalAccessories.ODTOA_kid, Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Master.Product_Kid, OtherDetail_OptionalAccessories.ODTOA_IsDeleted, Product_Master.Product_IsDeleted FROM OtherDetail_OptionalAccessories INNER JOIN Product_Master ON OtherDetail_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_Kid WHERE (Product_Master.Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (OtherDetail_OptionalAccessories.ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND(OtherDetail_OptionalAccessories.ODTOA_IsDeleted = N'0') AND (Product_Master.Product_IsDeleted = N'0')", gvOptionalAccesories)
            CreateMessageAlert(Me, "Record Saved Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            SaveClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Function GenerateOptionalAcceCode() As String
        ' generate supplier factory details code and kid
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "OAC" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from OtherDetail_OptionalAccessories where ODTOA_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Function

    Protected Sub gvOptionalAccesories_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvOptionalAccesories.SelectedIndexChanged

        Try

            Dim kid As String
            kid = ReturnValue("Select ODTOA_OAccessId from OtherDetail_OptionalAccessories where ODTOA_KId ='" & RemoveLiterals(gvOptionalAccesories.SelectedDataKey.Value.ToString.Trim) & "'and ODTOA_IsDeleted = '0'")
            'ddlOptionalAccessories.ClearSelection()
            ddlOptionalAccessories.ClearSelection()
            ddlOptionalAccessories.Items.FindByValue(RemoveLiterals(kid.ToString.Trim)).Selected = True
            GridClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
            ddlOptionalAccessories.Enabled = True

            If Session("AdminID") <> Nothing Then
                ddlOptionalAccessories.Enabled = True
            Else
                ddlOptionalAccessories.Enabled = False
            End If

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnOptionAccUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOptionAccUpdate.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM OtherDetail_OptionalAccessories WHERE (ODTOA_IsDeleted = '0') AND (ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ODTOA_OAccessId = '" & RemoveLiterals(ddlOptionalAccessories.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Optional Accessory is Already Present.", lblOptionalMsg)
                'ddlOptionalAccessories.ClearSelection()
                ScriptManager1.SetFocus(ddlOptionalAccessories) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ODTOA_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(gvOptionalAccesories.SelectedDataKey.Value.ToString.Trim)
            sqlcoll.Add("@ODTOA_Code", Data.SqlDbType.NVarChar, 25).Value = ReturnValue("Select ODTOA_code from OtherDetails_OptionalAccessories where ODTOA_kid = '" & RemoveLiterals(gvOptionalAccesories.SelectedDataKey.Value.ToString.Trim) & "'and ODTOA_IsDeleted ='0'")
            sqlcoll.Add("@ODTOA_ProductId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ODTOA_OAccessId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlOptionalAccessories.SelectedItem.Value)
            sqlcoll.Add("@ODTOA_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ODTOA_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ODTOA_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Update"
            ExecQuery("OtherDetail_OptionalAccessories_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Updated Successfully.", lblOptionalMsg)
            ddlOptionalAccessories.ClearSelection()
            ddlOptionalAccessories.Enabled = False
            FillOptionalGrid()
            lblOptDetails.Visible = True
            'Fill_Grid("SELECT OtherDetail_OptionalAccessories.ODTOA_kid, Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Master.Product_Kid, OtherDetail_OptionalAccessories.ODTOA_IsDeleted, Product_Master.Product_IsDeleted FROM OtherDetail_OptionalAccessories INNER JOIN Product_Master ON OtherDetail_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_Kid WHERE (Product_Master.Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (OtherDetail_OptionalAccessories.ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND(OtherDetail_OptionalAccessories.ODTOA_IsDeleted = N'0') AND (Product_Master.Product_IsDeleted = N'0')", gvOptionalAccesories)
            CreateMessageAlert(Me, "Record Updated Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            EditClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnOptionAccDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOptionAccDelete.Click

        Try

            If ddlOptionalAccessories.SelectedIndex > 0 Then
                ExecuteQuery("Update OtherDetail_OptionalAccessories set ODTOA_isdeleted ='1' where ODTOA_kid = '" & RemoveLiterals(gvOptionalAccesories.SelectedDataKey.Value.ToString.Trim) & "'")
                showmessage("Record Deleted Successfully.", lblOptionalMsg)
                ddlOptionalAccessories.ClearSelection()
                ddlOptionalAccessories.Enabled = False
                DeleteClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
                FillOptionalGrid()
                If gvOptionalAccesories.Rows.Count = 0 Then
                    lblOptDetails.Visible = False
                Else
                    lblOptDetails.Visible = True
                End If
                CreateMessageAlert(Me, "Record Deleted Successfully.", "StrKeyVal")
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnOptionAccCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOptionAccCancel.Click

        Try

            CancelClick(Me, btnOptionAccAdd, btnOptionAccSave, btnOptionAccUpdate, btnOptionAccDelete, btnOptionAccCancel, btnOptionAccSearch)
            ddlOptionalAccessories.ClearSelection()
            ddlOptionalAccessories.Enabled = False
        Catch ex As Exception

        End Try

    End Sub
    Protected Function GenerateIncludedAcceCode() As String
        ' generate supplier factory details code and kid
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "IAC" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from OtherDetail_IncludedAccessories where ODTIA_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Function
    Protected Sub btnIncludedAcceSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncludedAcceSave.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM OtherDetail_IncludedAccessories WHERE (ODTIA_IsDeleted = '0') AND (ODTIA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ODTIA_InAccessId = '" & RemoveLiterals(ddlIncludedAccessories.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Included Accessory is Already Present.", lblIncludedMsg)
                'ddlOptionalAccessories.ClearSelection()
                ScriptManager1.SetFocus(ddlIncludedAccessories) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ODTIA_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(NewPrimaryKey(Me))
            sqlcoll.Add("@ODTIA_Code", Data.SqlDbType.NVarChar, 25).Value = GenerateIncludedAcceCode().ToString
            sqlcoll.Add("@ODTIA_ProductId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ODTIA_InAccessId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlIncludedAccessories.SelectedItem.Value)
            sqlcoll.Add("@ODTIA_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ODTIA_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ODTIA_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Insert"
            ExecQuery("OtherDetail_IncludedAccessories_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Saved Successfully.", lblIncludedMsg)
            ddlIncludedAccessories.ClearSelection()
            ddlIncludedAccessories.Enabled = False
            FillIncludedGrid()
            lblIncDetails.Visible = True
            'Fill_Grid("SELECT OtherDetail_OptionalAccessories.ODTOA_kid, Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Master.Product_Kid, OtherDetail_OptionalAccessories.ODTOA_IsDeleted, Product_Master.Product_IsDeleted FROM OtherDetail_OptionalAccessories INNER JOIN Product_Master ON OtherDetail_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_Kid WHERE (Product_Master.Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (OtherDetail_OptionalAccessories.ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND(OtherDetail_OptionalAccessories.ODTOA_IsDeleted = N'0') AND (Product_Master.Product_IsDeleted = N'0')", gvOptionalAccesories)
            CreateMessageAlert(Me, "Record Saved Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            SaveClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnIncludedAcceUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncludedAcceUpdate.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM OtherDetail_IncludedAccessories WHERE (ODTIA_IsDeleted = '0') AND (ODTIA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ODTIA_InAccessId = '" & RemoveLiterals(ddlIncludedAccessories.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Included Accessory is Already Present.", lblIncludedMsg)
                'ddlOptionalAccessories.ClearSelection()
                ScriptManager1.SetFocus(ddlIncludedAccessories) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ODTIA_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(gvIncludedAccessories.SelectedDataKey.Value.ToString.Trim)
            sqlcoll.Add("@ODTIA_Code", Data.SqlDbType.NVarChar, 25).Value = ReturnValue("Select ODTIA_code from OtherDetail_IncludedAccessories where ODTIA_kid = '" & RemoveLiterals(gvIncludedAccessories.SelectedDataKey.Value.ToString.Trim) & "'and ODTIA_IsDeleted ='0'")
            sqlcoll.Add("@ODTIA_ProductId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ODTIA_InAccessId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlIncludedAccessories.SelectedItem.Value)
            sqlcoll.Add("@ODTIA_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ODTIA_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ODTIA_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Update"
            ExecQuery("OtherDetail_IncludedAccessories_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Updated Successfully.", lblIncludedMsg)
            ddlIncludedAccessories.ClearSelection()
            ddlIncludedAccessories.Enabled = False
            FillIncludedGrid()
            lblIncDetails.Visible = True
            'Fill_Grid("SELECT OtherDetail_OptionalAccessories.ODTOA_kid, Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Master.Product_Kid, OtherDetail_OptionalAccessories.ODTOA_IsDeleted, Product_Master.Product_IsDeleted FROM OtherDetail_OptionalAccessories INNER JOIN Product_Master ON OtherDetail_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_Kid WHERE (Product_Master.Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (OtherDetail_OptionalAccessories.ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND(OtherDetail_OptionalAccessories.ODTOA_IsDeleted = N'0') AND (Product_Master.Product_IsDeleted = N'0')", gvOptionalAccesories)
            CreateMessageAlert(Me, "Record Updated Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            EditClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnIncludedAcceDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncludedAcceDelete.Click

        Try

            If ddlIncludedAccessories.SelectedIndex > 0 Then
                ExecuteQuery("Update OtherDetail_IncludedAccessories set ODTIA_isdeleted ='1' where ODTIA_kid = '" & RemoveLiterals(gvIncludedAccessories.SelectedDataKey.Value.ToString.Trim) & "'")
                showmessage("Record Deleted Successfully.", lblIncludedMsg)
                ddlIncludedAccessories.ClearSelection()
                ddlIncludedAccessories.Enabled = False
                DeleteClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
                FillIncludedGrid()
                If gvIncludedAccessories.Rows.Count = 0 Then
                    lblIncDetails.Visible = False
                Else
                    lblIncDetails.Visible = True
                End If
                CreateMessageAlert(Me, "Record Deleted Successfully.", "StrKeyVal")
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnIncludedAcceCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncludedAcceCancel.Click

        Try

            CancelClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
            ddlIncludedAccessories.ClearSelection()
            ddlIncludedAccessories.Enabled = False
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub gvIncludedAccessories_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvIncludedAccessories.SelectedIndexChanged

        Try

            Dim kid As String
            kid = ReturnValue("Select ODTIA_InAccessId from OtherDetail_IncludedAccessories where ODTIA_KId ='" & RemoveLiterals(gvIncludedAccessories.SelectedDataKey.Value.ToString.Trim) & "'and ODTIA_IsDeleted = '0'")
            'ddlOptionalAccessories.ClearSelection()
            ddlIncludedAccessories.ClearSelection()
            ddlIncludedAccessories.Items.FindByValue(RemoveLiterals(kid.ToString.Trim)).Selected = True
            GridClick(Me, btnIncludedAcceAdd, btnIncludedAcceSave, btnIncludedAcceUpdate, btnIncludedAcceDelete, btnIncludedAcceCancel, btnIncludedAcceSearch)
            ddlIncludedAccessories.Enabled = True

            If Session("AdminID") <> Nothing Then
                ddlIncludedAccessories.Enabled = True
            Else
                ddlIncludedAccessories.Enabled = False
            End If

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnSparePartsAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSparePartsAdd.Click

        Try

            ddlSpareParts.Enabled = True
            AddClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
        Catch ex As Exception

        End Try

    End Sub
    Protected Function GenerateSparePartsCode() As String
        ' generate supplier factory details code and kid
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "IAC" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from OtherDetail_SpareParts where ODTSP_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Function
    Protected Sub btnSparePartsSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSparePartsSave.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM OtherDetail_SpareParts WHERE (ODTSP_IsDeleted = '0') AND (ODTSP_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ODTSP_SparePartId = '" & RemoveLiterals(ddlSpareParts.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Spare Parts is Already Present.", lblSparePartsMsg)
                'ddlOptionalAccessories.ClearSelection()
                ScriptManager1.SetFocus(ddlSpareParts) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ODTSP_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(NewPrimaryKey(Me))
            sqlcoll.Add("@ODTSP_Code", Data.SqlDbType.NVarChar, 25).Value = GenerateSparePartsCode().ToString
            sqlcoll.Add("@ODTSP_ProductId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ODTSP_SparePartId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlSpareParts.SelectedItem.Value)
            sqlcoll.Add("@ODTSP_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ODTSP_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ODTSP_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Insert"
            ExecQuery("OtherDetail_SpareParts_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Saved Successfully.", lblSparePartsMsg)
            ddlSpareParts.ClearSelection()
            ddlSpareParts.Enabled = False
            FillSparePartsGrid()
            lblSparePartsDetails.Visible = True
            'Fill_Grid("SELECT OtherDetail_OptionalAccessories.ODTOA_kid, Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Master.Product_Kid, OtherDetail_OptionalAccessories.ODTOA_IsDeleted, Product_Master.Product_IsDeleted FROM OtherDetail_OptionalAccessories INNER JOIN Product_Master ON OtherDetail_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_Kid WHERE (Product_Master.Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (OtherDetail_OptionalAccessories.ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND(OtherDetail_OptionalAccessories.ODTOA_IsDeleted = N'0') AND (Product_Master.Product_IsDeleted = N'0')", gvOptionalAccesories)
            CreateMessageAlert(Me, "Record Saved Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            SaveClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnSparePartsUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSparePartsUpdate.Click

        Try

            If IsAlreadyPresent("SELECT COUNT(*) FROM OtherDetail_SpareParts WHERE (ODTSP_IsDeleted = '0') AND (ODTSP_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (ODTSP_SparePartId = '" & RemoveLiterals(ddlSpareParts.SelectedItem.Value.ToString.Trim) & "')") Then
                showmessage("Spare Parts is Already Present.", lblSparePartsMsg)
                'ddlOptionalAccessories.ClearSelection()
                ScriptManager1.SetFocus(ddlSpareParts) 'Put the Focus to branchName TextField
                Exit Sub
            End If

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@ODTSP_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(gvSpareParts.SelectedDataKey.Value.ToString.Trim)
            sqlcoll.Add("@ODTSP_Code", Data.SqlDbType.NVarChar, 25).Value = ReturnValue("Select ODTSP_code from OtherDetail_SpareParts where ODTSP_kid = '" & RemoveLiterals(gvSpareParts.SelectedDataKey.Value.ToString.Trim) & "'and ODTSP_IsDeleted ='0'")
            sqlcoll.Add("@ODTSP_ProductId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("ProductID").ToString.Trim)
            sqlcoll.Add("@ODTSP_SparePartId", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(ddlSpareParts.SelectedItem.Value)
            sqlcoll.Add("@ODTSP_FinancialYearId", Data.SqlDbType.NVarChar, 10).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@ODTSP_CompanyID", Data.SqlDbType.NVarChar, 10).Value = Session("g_Company").ToString
            sqlcoll.Add("@ODTSP_UserID", Data.SqlDbType.NVarChar, 10).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.NVarChar, 10).Value = "Update"
            ExecQuery("OtherDetail_SpareParts_Proc", sqlcoll)
            sqlcoll.Clear()

            showmessage("Record Updated Successfully.", lblSparePartsMsg)
            ddlSpareParts.ClearSelection()
            ddlSpareParts.Enabled = False
            FillSparePartsGrid()
            lblSparePartsDetails.Visible = True
            'Fill_Grid("SELECT OtherDetail_OptionalAccessories.ODTOA_kid, Product_Master.Product_Name, Product_Master.Product_Code, Product_Master.Product_ModelNo, Product_Master.Product_Kid, OtherDetail_OptionalAccessories.ODTOA_IsDeleted, Product_Master.Product_IsDeleted FROM OtherDetail_OptionalAccessories INNER JOIN Product_Master ON OtherDetail_OptionalAccessories.ODTOA_ProductId = Product_Master.Product_Kid WHERE (Product_Master.Product_Kid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (OtherDetail_OptionalAccessories.ODTOA_ProductId = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND(OtherDetail_OptionalAccessories.ODTOA_IsDeleted = N'0') AND (Product_Master.Product_IsDeleted = N'0')", gvOptionalAccesories)
            CreateMessageAlert(Me, "Record Updated Successfully.", "StrKeyVal")
            'enableconcontrol(False)
            'lblConField.Visible = False
            'clearconfields()
            'Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            ' Fill_Grid("SELECT Client_Master.ctm_Kid, Client_Master.ctm_name + ' - ' + City_Master.City_Name AS [Client Name] FROM Client_Master INNER JOIN City_Master ON Client_Master.ctm_City = City_Master.City_Kid WHERE (Client_Master.ctm_SupplierId = '" & Session("SupplierID").ToString.Trim & "')AND (Client_Master.ctm_productid = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (Client_Master.ctm_IsDeleted = '0') AND (City_Master.City_IsDeleted = '0')", gvClient)
            EditClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnSparePartsDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSparePartsDelete.Click

        Try

            If ddlSpareParts.SelectedIndex > 0 Then
                ExecuteQuery("Update OtherDetail_SpareParts set ODTSP_isdeleted ='1' where ODTSP_kid = '" & RemoveLiterals(gvSpareParts.SelectedDataKey.Value.ToString.Trim) & "'")
                showmessage("Record Deleted Successfully.", lblSparePartsMsg)
                ddlSpareParts.ClearSelection()
                ddlSpareParts.Enabled = False
                DeleteClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
                FillSparePartsGrid()
                If gvSpareParts.Rows.Count = 0 Then
                    lblSparePartsDetails.Visible = False
                Else
                    lblSparePartsDetails.Visible = True
                End If
                CreateMessageAlert(Me, "Record Deleted Successfully.", "StrKeyVal")
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnSparePartsCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSparePartsCancel.Click

        Try

            CancelClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
            ddlSpareParts.ClearSelection()
            ddlSpareParts.Enabled = False
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub gvSpareParts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSpareParts.SelectedIndexChanged

        Try

            Dim kid As String
            kid = ReturnValue("Select ODTSP_SparePartId from OtherDetail_SpareParts where ODTSP_KId ='" & RemoveLiterals(gvSpareParts.SelectedDataKey.Value.ToString.Trim) & "'and ODTSP_IsDeleted = '0'")
            'ddlOptionalAccessories.ClearSelection()
            ddlSpareParts.ClearSelection()
            ddlSpareParts.Items.FindByValue(RemoveLiterals(kid.ToString.Trim)).Selected = True
            GridClick(Me, btnSparePartsAdd, btnSparePartsSave, btnSparePartsUpdate, btnSparePartsDelete, btnSparePartsCancel, btnSparePartsSearch)
            ddlSpareParts.Enabled = True
            If Session("AdminID") <> Nothing Then
                ddlSpareParts.Enabled = True
            Else
                ddlSpareParts.Enabled = False
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnFactory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFactory.Click
        'Dim i As Integer
        'i = Mtv.ActiveViewIndex
        Response.Redirect("supplier-factory-master.aspx")
    End Sub

    Protected Sub btnWareHouse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWareHouse.Click
        Response.Redirect("supplier-warehouse-master.aspx")
    End Sub

    Protected Sub btnClient_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClient.Click
        Response.Redirect("client-master.aspx")
    End Sub

    Protected Sub btnBank_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBank.Click
        Response.Redirect("bank-details.aspx")
    End Sub

    Protected Sub btnBranch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBranch.Click
        Response.Redirect("branch-network-master.aspx")
    End Sub

    Protected Sub btnServiceCenter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnServiceCenter.Click
        'CreateMessageAlert(Me, "asdasdasdasd", "StrKeyVal")
        Response.Redirect("after-sales-service.aspx")
    End Sub

    'Protected Sub ddlBillingFrom_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
    '    If ddlBillingFrom.SelectedItem.Text = "Add New Factory" Then
    '        'ddlBillingFrom.Attributes.Add("OnSelectedIndexChanged", "return DeleteMsg();")
    '        Response.Redirect("supplier-factory-master.aspx?index=" & Mtv.ActiveViewIndex)
    '    ElseIf ddlBillingFrom.SelectedItem.Text = "Add New WareHouse" Then
    '        Response.Redirect("supplier-warehouse-master.aspx?index=" & Mtv.ActiveViewIndex)
    '    End If
    'End Sub

    'Protected Sub ddlClient_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    If ddlClient.SelectedItem.Text = "Add New Client" Then
    '        Response.Redirect("client-master.aspx?index=" & Mtv.ActiveViewIndex)
    '    End If
    'End Sub
End Class
