
' Developer -> Jignesh Jitiya


Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class billingfrom
    Inherits System.Web.UI.Page
    Shared g_User As String
    Shared g_Company As String
    Shared g_FinYear As String
    Shared Category(10) As String
    Shared i As Integer
    Shared Billingkid As String
    Shared Billingcode As String
    Shared City_Kid As String
    Shared City_Code As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then

            Try
                'g_User = Session("g_User").ToString
                'g_Company = Session("g_Company").ToString
                'g_FinYear = Session("g_FinYear").ToString
                If Session("ProductID") = Nothing Then ' Check whether ProductID Created Or Not
                    Response.Redirect("suppliers/List_Product.aspx")
                Else
                    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_CompanyId ='" & Session("g_Company").ToString & "'")
                    ' Get CategoryID According To product id
                    FillCategoryName(catid)
                    ' fill category herarchy according to catid
                    FillCombo() ' fill country, state and city combo
                    getexisting() ' get exsting data if any
                    lblrbl.Visible = False
                    ddlrbl.Visible = False
                End If
                If Session("AdminID") <> Nothing Then ' check whether admin 
                    btnUpdate.Visible = True
                    If ddlExisting.Visible = True Then
                        BtnSave.Visible = True
                        btnUpdate.Visible = False
                        enablecontrols(False)
                    Else
                        enablecontrols(True)
                    End If
                Else
                    btnUpdate.Visible = False
                End If
                If ddlExisting.Visible = False Then
                    ImageButton4.Visible = False
                End If
            Catch ex As Exception
                CreateMessageAlert(Me, ex.Message, "StrKeyVal")
            End Try
        End If
        BtnAdd.Visible = False
        txtCity.Visible = False
        ddlCity_Code.Visible = True

    End Sub
    Public Sub getexisting()
        Dim dt As DataTable
        dt = ReturnDataTable("SELECT Country_Master.Country_Name, Country_Master.Country_KID, BillingFrom.*, State_Master.State_Name, State_Master.State_KID, City_Master.City_Name, City_Master.City_KID, Product_Master.Product_Name FROM BillingFrom INNER JOIN City_Master ON BillingFrom.BillingFrom_LocationId = City_Master.City_Kid INNER JOIN State_Master ON City_Master.City_StateId = State_Master.State_Kid INNER JOIN Country_Master ON State_Master.State_CountryId = Country_Master.Country_Kid INNER JOIN Product_Master ON BillingFrom.BillingFrom_ProductID = Product_Master.Product_Kid WHERE (BillingFrom.BillingFrom_ProductID = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (BillingFrom.BillingFrom_IsDeleted = '0')")
        If dt.Rows.Count <> 0 Then
            ' if data is already exist
            BtnSave.Visible = False
            rblFrom.Enabled = False
            lblExisting.Visible = False
            ddlExisting.Visible = False
            enablecontrols(False)
            Billingkid = dt.Rows(0).Item("BillingFrom_Kid").ToString()
            Billingcode = dt.Rows(0).Item("BillingFrom_Code").ToString()
            rblFrom.ClearSelection()
            Try
                rblFrom.SelectedValue = dt.Rows(0).Item("BillingFrom_From").ToString()
            Catch ex As Exception
                rblFrom.SelectedValue = "O"
            End Try
            txtFromType.Text = dt.Rows(0).Item("BillingFrom_type").ToString
            txtFromName.Text = dt.Rows(0).Item("BillingFrom_Name").ToString
            FillCombo()
            ddlCountry_Code.ClearSelection()
            Try
                ddlCountry_Code.Items.FindByValue(dt.Rows(0).Item("Country_Kid")).Selected = True
            Catch ex As Exception
            End Try
            ddlState_Code.ClearSelection()
            Try
                ddlState_Code.Items.FindByValue(dt.Rows(0).Item("State_Kid")).Selected = True
            Catch ex As Exception
            End Try
            Fill_Combo("SELECT City_Kid, City_Name FROM City_Master WHERE City_StateID ='" & ddlState_Code.SelectedItem.Value & "'and City_CompanyId = '" & Session("g_Company").ToString & "' and City_IsDeleted = '0'", ddlCity_Code)
            ddlCity_Code.ClearSelection()
            Try
                ddlCity_Code.Items.FindByValue(dt.Rows(0).Item("City_Kid")).Selected = True
            Catch ex As Exception
            End Try
            txtAddress.Text = dt.Rows(0).Item("BillingFrom_Address").ToString
            txtPinCode.Text = dt.Rows(0).Item("BillingFrom_PinCode").ToString

            Dim pcountry1, pcity1, phoneno1, mcountry1, mobile1, fcountry1, fcity1, faxno1 As String

            Try
                phoneno1 = dt.Rows(0).Item("BillingFrom_PhoneNo").ToString
                If phoneno1.Contains("-") Then
                    pcountry1 = phoneno1.Substring(0, phoneno1.IndexOf("-"))
                    phoneno1 = phoneno1.Substring(phoneno1.IndexOf("-") + 1)
                    If phoneno1.Contains("-") Then
                        pcity1 = phoneno1.Substring(0, phoneno1.IndexOf("-"))
                        phoneno1 = phoneno1.Substring(phoneno1.IndexOf("-") + 1)
                        txtCountryCode.Text = pcountry1
                        txtCityCode.Text = pcity1
                        txtPhoneNo.Text = phoneno1
                    Else
                        txtCityCode.Text = pcountry1
                        txtPhoneNo.Text = phoneno1
                    End If

                Else
                    txtPhoneNo.Text = phoneno1
                End If

            Catch ex As Exception
                txtCountryCode.Text = ""
                txtCityCode.Text = ""
                txtPhoneNo.Text = ""
            End Try

            Try
                mobile1 = dt.Rows(0).Item("BillingFrom_MobileNo").ToString
                If mobile1.Contains("-") Then
                    mcountry1 = mobile1.Substring(0, mobile1.IndexOf("-"))
                    mobile1 = mobile1.Substring(mobile1.IndexOf("-") + 1)
                    txtCountryCodee.Text = mcountry1
                    txtMobileNo.Text = mobile1
                Else
                    txtMobileNo.Text = mobile1
                End If

            Catch ex As Exception
                txtCountryCodee.Text = ""
                txtMobileNo.Text = ""
            End Try

            Try
                If IsDBNull(dt.Rows(0).Item("BillingFrom_FaxNo")) Then
                Else
                    faxno1 = dt.Rows(0).Item("BillingFrom_FaxNo").ToString
                    If faxno1.Contains("-") Then
                        fcountry1 = faxno1.Substring(0, faxno1.IndexOf("-"))
                        faxno1 = faxno1.Substring(faxno1.IndexOf("-") + 1)
                        If faxno1.Contains("-") Then
                            fcity1 = faxno1.Substring(0, faxno1.IndexOf("-"))
                            faxno1 = faxno1.Substring(faxno1.IndexOf("-") + 1)
                            txtCountryCodeF.Text = fcountry1
                            txtCityCodeF.Text = fcity1
                            txtFaxNo.Text = faxno1
                        Else
                            txtCityCodeF.Text = fcountry1
                            txtFaxNo.Text = faxno1
                        End If

                    Else
                        txtFaxNo.Text = faxno1
                    End If

                End If
            Catch ex As Exception
                txtCountryCodeF.Text = ""
                txtCityCodeF.Text = ""
                txtFaxNo.Text = ""
            End Try

            txtContactPerson.Text = dt.Rows(0).Item("BillingFrom_ContactPerson").ToString
            txtPersonDesignation.Text = dt.Rows(0).Item("BillingFrom_PersonDesignation").ToString
            txtEmailId.Text = dt.Rows(0).Item("BillingFrom_EmailID").ToString
            txtPhoneExtention.Text = dt.Rows(0).Item("BillingFrom_PhoneExtn").ToString()

        Else
            ' if not get existing data
            ddlExisting.Visible = True
            lblExisting.Visible = True
            enablecontrols(FillExistingDropDown("SELECT BillingFrom_productid FROM BillingFrom WHERE BillingFrom_userid = '" & Session("g_User").ToString.Trim & "' AND BillingFrom_companyid = '" & Session("g_Company").ToString & "' AND BillingFrom_IsDeleted = N'0'", ddlExisting, lblExisting))
            rblFrom.Enabled = True
            BtnSave.Visible = True
        End If
        dt.Clear()
    End Sub
    Public Function FillExistingDropDown(ByVal strQuery As String, ByVal ddl As DropDownList, ByVal lbl As Label) As Boolean
        ' Fill Product name in ddl for existing value
        Dim dt1 As DataTable = getTable(strQuery, "PR")
        Dim i As Integer
        Dim Kid As String
        Dim KidBool As Boolean = True

        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

        Fill_Combo("Select Product_Kid,Product_Name from Product_Master where Product_SupplierID='" & Session.Item("SupplierID").ToString.Trim & "'and Product_Kid In ( " & Kid & ")", ddl)
        ' fill product name from product master

        If ddl.Items.Count > 1 Then
            ddl.Items.Insert(ddl.Items.Count, "Add New")
            lbl.Visible = True
            Return False
            'unLockControlSalesContact(False)
        Else
            lbl.Visible = False
            ddl.Visible = False
            Return True
            'unLockControlSalesContact(True)
        End If
        dt1.Clear()

    End Function
    Private Sub FillCombo()
        ' fill combo for country, state and city and also assign value in country code text boxes
        Fill_Combo("SELECT distinct Country_Kid, Country_Name FROM Country_Master where Country_IsDeleted='0'and Country_CompanyId='" & Session("g_Company").ToString & "' order by Country_Name", ddlCountry_Code)
        ddlCountry_Code.ClearSelection()
        ddlCountry_Code.Items.FindByText("India").Selected = True
        Fill_Combo("SELECT State_Master.State_Kid, State_Master.State_Name, Country_Master.Country_Name FROM Country_Master INNER JOIN State_Master ON Country_Master.Country_Kid = State_Master.State_CountryId WHERE State_Master.State_CompanyId = '" & Session("g_Company").ToString & "' AND State_Master.State_IsDeleted = '0' AND Country_Master.Country_Kid = '" & ddlCountry_Code.SelectedItem.Value & "'", ddlState_Code)
        txtCountryCode.Text = ReturnValue("Select Country_ISDCode from Country_Master where Country_Isdeleted ='0' and Country_Kid ='" & ddlCountry_Code.SelectedItem.Value & "'and Country_CompanyId ='" & Session("g_Company").ToString & "'")
        txtCountryCodee.Text = ReturnValue("Select Country_ISDCode from Country_Master where Country_Isdeleted ='0' and Country_Kid ='" & ddlCountry_Code.SelectedItem.Value & "'and Country_CompanyId ='" & Session("g_Company").ToString & "'")
        txtCountryCodeF.Text = ReturnValue("Select Country_ISDCode from Country_Master where Country_Isdeleted ='0' and Country_Kid ='" & ddlCountry_Code.SelectedItem.Value & "'and Country_CompanyId ='" & Session("g_Company").ToString & "'")
        ddlCity_Code.Items.Clear()
    End Sub
    Public Sub FillCategoryName(ByVal Catid As String)
        ' fill category herarchy in lable 
        Dim dt As DataTable
        dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0' and Category_FinancialYearId ='" & Session("g_FinYear").ToString & "' and Category_CompanyId ='" & Session("g_Company").ToString & "'")
        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()
    End Sub

    Protected Sub ddlCountry_Code_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountry_Code.SelectedIndexChanged
        Try
            txtCountryCode.Text = ReturnValue("Select Country_ISDCode from Country_Master where Country_Isdeleted ='0' and Country_Kid ='" & ddlCountry_Code.SelectedItem.Value & "'and Country_CompanyId ='" & Session("g_Company").ToString & "'")
            If txtCountryCode.Text = "" Then
                txtCountryCode.Enabled = True
            Else
                txtCountryCode.Enabled = False
            End If
            txtCountryCodee.Text = ReturnValue("Select Country_ISDCode from Country_Master where Country_Isdeleted ='0' and Country_Kid ='" & ddlCountry_Code.SelectedItem.Value & "'and Country_CompanyId ='" & Session("g_Company").ToString & "'")
            If txtCountryCodee.Text = "" Then
                txtCountryCodee.Enabled = True
            Else
                txtCountryCodee.Enabled = False
            End If
            If ddlCountry_Code.SelectedIndex > 0 Then
                Fill_Combo("Select State_Kid,State_Name from State_Master where State_IsDeleted='0' and state_CountryId='" & ddlCountry_Code.SelectedValue.Trim & "'and State_CompanyId='" & Session("g_Company").ToString.Trim & "'", ddlState_Code)
            End If
            txtCountryCodeF.Text = ReturnValue("Select Country_ISDCode from Country_Master where Country_Isdeleted ='0' and Country_Kid ='" & ddlCountry_Code.SelectedItem.Value & "'and Country_CompanyId ='" & Session("g_Company").ToString & "'")
            If txtCountryCodeF.Text = "" Then
                txtCountryCodeF.Enabled = True
            Else
                txtCountryCodeF.Enabled = False
            End If
            If ddlCountry_Code.SelectedIndex > 0 Then
                Fill_Combo("Select State_Kid,State_Name from State_Master where State_IsDeleted='0' and state_CountryId='" & ddlCountry_Code.SelectedValue.Trim & "'and State_CompanyId='" & Session("g_Company").ToString.Trim & "'", ddlState_Code)
            End If
            ddlCity_Code.Items.Clear()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub ddlState_Code_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlState_Code.SelectedIndexChanged
        Try
            If ddlState_Code.SelectedIndex > 0 Then
                ddlCity_Code.Visible = True
                Fill_Combo("Select City_Kid,City_Name from City_Master where City_IsDeleted='0' and City_StateId='" & ddlState_Code.SelectedValue.Trim & "'", ddlCity_Code)
                ddlCity_Code.Items.Add("Others") ' For add new city
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub ddlCity_Code_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCity_Code.SelectedIndexChanged
        If ddlCity_Code.SelectedItem.Text = "Others" Then
            ' if want to add new city
            txtCity.Visible = True
            BtnAdd.Visible = True
            ddlCity_Code.Visible = False
        Else
            txtCity.Visible = False
            BtnAdd.Visible = False
        End If
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        ' save facilities
        GenerateBillingCode() ' generate code and kid
        Dim Con As New SqlConnection
        Dim cmd As New SqlCommand
        Con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        cmd.Connection = Con
        cmd.CommandText = "BillingFrom_Proc" ' store procedure
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Kid", Data.SqlDbType.NVarChar, 10)).Value = Billingkid
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Code", Data.SqlDbType.NVarChar, 25)).Value = Billingcode
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_From", Data.SqlDbType.NVarChar, 1)).Value = rblFrom.SelectedItem.Value

        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Type", Data.SqlDbType.NVarChar, 50)).Value = txtFromType.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Name", Data.SqlDbType.NVarChar, 50)).Value = txtFromName.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_LocationId", Data.SqlDbType.NVarChar, 10)).Value = ddlCity_Code.SelectedValue
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Address", Data.SqlDbType.NVarChar, 150)).Value = txtAddress.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PinCode", Data.SqlDbType.NVarChar, 15)).Value = txtPinCode.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PhoneNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCode.Text & "-" & txtCityCode.Text & "-" & txtPhoneNo.Text

        If txtCountryCodeF.Text = "" Or txtCityCodeF.Text = "" Or txtFaxNo.Text = "" Then
        Else
            cmd.Parameters.Add(New SqlParameter("@BillingFrom_FaxNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCodeF.Text & "-" & txtCityCodeF.Text & "-" & txtFaxNo.Text
        End If
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_ContactPerson", Data.SqlDbType.NVarChar, 100)).Value = txtContactPerson.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PersonDesignation", Data.SqlDbType.NVarChar, 50)).Value = txtPersonDesignation.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PhoneExtn", Data.SqlDbType.NVarChar, 15)).Value = txtPhoneExtention.Text
        If txtCountryCodee.Text = "" Or txtMobileNo.Text = "" Then
        Else
            cmd.Parameters.Add(New SqlParameter("@BillingFrom_MobileNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCodee.Text & "-" & txtMobileNo.Text
        End If
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_EmailID", Data.SqlDbType.NVarChar, 50)).Value = txtEmailId.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_ProductID", Data.SqlDbType.NVarChar, 10)).Value = Session("ProductID").ToString()
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_CompanyID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_Company").ToString
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_FinancialYearId", Data.SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_UserID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_User").ToString
        cmd.Parameters.Add(New SqlParameter("@FormName", Data.SqlDbType.NVarChar, 0)).Value = Me.Title.ToString
        cmd.Parameters.Add(New SqlParameter("@Mode", Data.SqlDbType.Char, 15)).Value = "Insert"

        If Con.State = ConnectionState.Open Then ' check connection
            Con.Close()
        End If
        Con.Open()
        cmd.ExecuteNonQuery()
        Con.Close() ' close connection
        Con.Dispose()
        Response.Redirect("InstoreServices.aspx") ' redirect to next page
    End Sub

    Protected Sub BtnSkip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSkip.Click
        Response.Redirect("InstoreServices.aspx") ' redirect to next page
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        ' update facilities
        Dim Con As New SqlConnection
        Dim cmd As New SqlCommand
        Con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        cmd.Connection = Con
        cmd.CommandText = "BillingFrom_Proc" ' Store Procedure
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Kid", Data.SqlDbType.NVarChar, 10)).Value = Billingkid
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Code", Data.SqlDbType.NVarChar, 25)).Value = Billingcode
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_From", Data.SqlDbType.NVarChar, 1)).Value = rblFrom.SelectedItem.Value
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Type", Data.SqlDbType.NVarChar, 50)).Value = txtFromType.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Name", Data.SqlDbType.NVarChar, 50)).Value = txtFromName.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_LocationId", Data.SqlDbType.NVarChar, 10)).Value = ddlCity_Code.SelectedValue
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Address", Data.SqlDbType.NVarChar, 150)).Value = txtAddress.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PinCode", Data.SqlDbType.NVarChar, 15)).Value = txtPinCode.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PhoneNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCode.Text & "-" & txtCityCode.Text & "-" & txtPhoneNo.Text

        If txtCountryCodeF.Text = "" Or txtCityCodeF.Text = "" Or txtFaxNo.Text = "" Then
        Else
            cmd.Parameters.Add(New SqlParameter("@BillingFrom_FaxNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCodeF.Text & "-" & txtCityCodeF.Text & "-" & txtFaxNo.Text
        End If
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_ContactPerson", Data.SqlDbType.NVarChar, 100)).Value = txtContactPerson.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PersonDesignation", Data.SqlDbType.NVarChar, 50)).Value = txtPersonDesignation.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PhoneExtn", Data.SqlDbType.NVarChar, 15)).Value = txtPhoneExtention.Text
        If txtCountryCodee.Text = "" Or txtMobileNo.Text = "" Then
        Else
            cmd.Parameters.Add(New SqlParameter("@BillingFrom_MobileNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCodee.Text & "-" & txtMobileNo.Text
        End If
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_EmailID", Data.SqlDbType.NVarChar, 50)).Value = txtEmailId.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_ProductID", Data.SqlDbType.NVarChar, 10)).Value = Session("ProductID").ToString()
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_CompanyID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_Company").ToString
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_FinancialYearId", Data.SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_UserID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_User").ToString
        cmd.Parameters.Add(New SqlParameter("@FormName", Data.SqlDbType.NVarChar, 0)).Value = Me.Title.ToString
        cmd.Parameters.Add(New SqlParameter("@Mode", Data.SqlDbType.Char, 15)).Value = "Update"
        If Con.State = ConnectionState.Open Then ' check connection whether opened
            Con.Close()
        End If
        Con.Open()
        cmd.ExecuteNonQuery()
        Con.Close() ' close connection
        Con.Dispose()
        Response.Redirect("InstoreServices.aspx") 'redirect to next page
    End Sub
    Protected Sub btnAddMore_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Add more facilities
        GenerateBillingCode() ' generate kid and code
        Dim Con As New SqlConnection
        Dim cmd As New SqlCommand
        Con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        cmd.Connection = Con
        cmd.CommandText = "BillingFrom_Proc" ' store procedure
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Kid", Data.SqlDbType.NVarChar, 10)).Value = Billingkid
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Code", Data.SqlDbType.NVarChar, 25)).Value = Billingcode
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_From", Data.SqlDbType.NVarChar, 1)).Value = rblFrom.SelectedItem.Value

        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Type", Data.SqlDbType.NVarChar, 50)).Value = txtFromType.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Name", Data.SqlDbType.NVarChar, 50)).Value = txtFromName.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_LocationId", Data.SqlDbType.NVarChar, 10)).Value = ddlCity_Code.SelectedValue
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_Address", Data.SqlDbType.NVarChar, 150)).Value = txtAddress.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PinCode", Data.SqlDbType.NVarChar, 15)).Value = txtPinCode.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PhoneNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCode.Text & "-" & txtCityCode.Text & "-" & txtPhoneNo.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_FaxNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCodeF.Text & "-" & txtCityCodeF.Text & "-" & txtFaxNo.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_ContactPerson", Data.SqlDbType.NVarChar, 100)).Value = txtContactPerson.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PersonDesignation", Data.SqlDbType.NVarChar, 50)).Value = txtPersonDesignation.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_PhoneExtn", Data.SqlDbType.NVarChar, 15)).Value = txtPhoneExtention.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_MobileNo", Data.SqlDbType.NVarChar, 25)).Value = txtCountryCodee.Text & "-" & txtMobileNo.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_EmailID", Data.SqlDbType.NVarChar, 50)).Value = txtEmailId.Text
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_ProductID", Data.SqlDbType.NVarChar, 10)).Value = Session("ProductID").ToString()
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_CompanyID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_Company").ToString
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_FinancialYearId", Data.SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString
        cmd.Parameters.Add(New SqlParameter("@BillingFrom_UserID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_User").ToString
        cmd.Parameters.Add(New SqlParameter("@FormName", Data.SqlDbType.NVarChar, 0)).Value = Me.Title.ToString
        cmd.Parameters.Add(New SqlParameter("@Mode", Data.SqlDbType.Char, 15)).Value = "Insert"
        If Con.State = ConnectionState.Open Then ' check whether connection is opened
            Con.Close()
        End If
        Con.Open()
        cmd.ExecuteNonQuery()
        Con.Close() ' connection cl0ose
        Con.Dispose()
        FillGrid("SELECT Country_Master.Country_Name, BillingFrom.*, State_Master.State_Name, City_Master.City_Name, Product_Master.Product_Name FROM BillingFrom INNER JOIN City_Master ON BillingFrom.BillingFrom_LocationId = City_Master.City_Kid INNER JOIN State_Master ON City_Master.City_StateId = State_Master.State_Kid INNER JOIN Country_Master ON State_Master.State_CountryId = Country_Master.Country_Kid INNER JOIN Product_Master ON BillingFrom.BillingFrom_ProductID = Product_Master.Product_Kid WHERE (BillingFrom.BillingFrom_ProductID = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (BillingFrom.BillingFrom_IsDeleted = '0')", gv1)
        'FillGrid("SELECT BillingFrom.*, BillingFrom.BillingFrom_LocationId AS Expr1, City_Master.City_Name, State_Master.State_Name, Country_Master.Country_Name, BillingFrom.BillingFrom_ProductID AS Expr2, BillingFrom.BillingFrom_IsDeleted AS Expr3 FROM BillingFrom INNER JOIN City_Master ON BillingFrom.BillingFrom_LocationId = City_Master.City_Kid INNER JOIN State_Master ON City_Master.City_StateId = State_Master.State_Kid INNER JOIN Country_Master ON State_Master.State_CountryId = Country_Master.Country_Kid WHERE (BillingFrom.BillingFrom_ProductID = '" & RemoveLiterals(Session("ProductID").ToString.Trim) & "') AND (BillingFrom.BillingFrom_IsDeleted = '0')", gv1)
        ' fill grid
        clearfields()
        FillCombo() ' fill Country, state and city ddl
    End Sub
    Public Sub FillGrid(ByVal StrQry As String, ByVal gv As GridView)
        ' fill grid according to StrQry
        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
            gv.Columns(1).Visible = True
            gv.DataSource = dr                                     'Set the DataReader Variable as DataSource for the Grid
            gv.DataBind()
            gv.Columns(1).Visible = False
            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
                If gv.Rows(i).Cells(2).Text = "O" Then
                    gv.Rows(i).Cells(2).Text = "Other"
                ElseIf gv.Rows(i).Cells(2).Text = "F" Then
                    gv.Rows(i).Cells(2).Text = "Factory"
                ElseIf gv.Rows(i).Cells(2).Text = "W" Then
                    gv.Rows(i).Cells(2).Text = "Ware House"
                End If
            Next
        Catch ex As Exception
            ' CreateMessageAlert(gv.Page, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        ' add new city
        GenerateCityCode() ' generate city code 
        Dim Con As New SqlConnection
        Dim cmd As New SqlCommand
        Con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        cmd.Connection = Con
        cmd.CommandText = "City_Master_Proc" ' store procedure
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@City_Kid", Data.SqlDbType.NVarChar, 10)).Value = City_Kid
        cmd.Parameters.Add(New SqlParameter("@City_Code", Data.SqlDbType.NVarChar, 25)).Value = City_Code
        cmd.Parameters.Add(New SqlParameter("@City_Name", Data.SqlDbType.NVarChar, 100)).Value = txtCity.Text
        cmd.Parameters.Add(New SqlParameter("@City_StateID", Data.SqlDbType.NVarChar, 10)).Value = ddlState_Code.SelectedValue
        cmd.Parameters.Add(New SqlParameter("@City_CompanyID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_Company").ToString
        cmd.Parameters.Add(New SqlParameter("@City_FinancialYearId", Data.SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString
        cmd.Parameters.Add(New SqlParameter("@City_UserID", Data.SqlDbType.NVarChar, 10)).Value = Session("g_User").ToString
        cmd.Parameters.Add(New SqlParameter("@FormName", Data.SqlDbType.NVarChar, 0)).Value = Me.Title.ToString
        cmd.Parameters.Add(New SqlParameter("@Mode", Data.SqlDbType.Char, 15)).Value = "Insert"
        If Con.State = ConnectionState.Open Then ' check whether connectio is opened or not
            Con.Close()
        End If
        Con.Open()
        cmd.ExecuteNonQuery()
        Con.Close() ' connection close
        Con.Dispose()
        ddlCity_Code.Visible = True
        Fill_Combo("Select City_KId, City_Name from City_Master where City_Isdeleted='0' and  City_StateId ='" & ddlState_Code.SelectedItem.Value & "' and City_CompanyId ='" & Session("g_Company").ToString & "'  order by City_Name", ddlCity_Code)
        ddlCity_Code.Items.FindByText(txtCity.Text).Selected = True
    End Sub
    Protected Sub GenerateCityCode()
        ' generate city code
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            City_Kid = NewPrimaryKey(Me)
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "CC" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from CityMaster where City_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    City_Code = code
                    Exit Sub
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub GenerateBillingCode()
        ' generate billing code
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            Billingkid = NewPrimaryKey(Me)
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "BF" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from BillingFrom where BillingFrom_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Billingcode = code
                    Exit Sub
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub rblFrom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If rblFrom.SelectedItem.Value = "F" Then
            lblrbl.Visible = True
            clearfields()
            fillfactory()
        ElseIf rblFrom.SelectedItem.Value = "W" Then
            lblrbl.Visible = True
            clearfields()
            fillwarehouse()
        ElseIf rblFrom.SelectedItem.Value = "O" Then
            clearfields()
            enablecontrols(True)
            lblrbl.Visible = False
            ddlrbl.Visible = False
            'fillother()
        End If
    End Sub
    Public Sub fillfactory()
        ' Fill Combo with factory name
        ddlrbl.Items.Clear()
        Fill_Combo("Select FactoryDetails_KId, FactoryDetails_Name from SupplierFactoryDetails where FactoryDetails_UserId = '" & RemoveLiterals(Session("g_User").ToString.Trim) & "'and FactoryDetails_Isdeleted='0'", ddlrbl)
        If ddlrbl.Items.Count <= 1 Then
            ' if records not found
            ddlrbl.Visible = False
            irbl.Visible = False
            lblrbl.Text = "No Factory Records Found."
            lblrbl.ForeColor = Drawing.Color.Red
            enablecontrols(False)
        Else
            ' if records found
            ddlrbl.Visible = True
            irbl.Visible = True
            lblrbl.Text = "Select Factory Name."
            lblrbl.ForeColor = Drawing.Color.Black
            enablecontrols(False)
        End If
    End Sub
    Public Sub fillwarehouse()
        ' fill combo with warenhouse name
        ddlrbl.Items.Clear()
        Fill_Combo("Select swi_KId, swi_whName from StockingWarehouseInformation where swi_UserId = '" & RemoveLiterals(Session("g_User").ToString.Trim) & "'and swi_Isdeleted='0'", ddlrbl)
        If ddlrbl.Items.Count <= 1 Then
            ' if records found
            ddlrbl.Visible = False
            irbl.Visible = False
            lblrbl.Text = "No WareHouse Records Found."
            lblrbl.ForeColor = Drawing.Color.Red
            enablecontrols(False)
        Else
            ' if records not found
            enablecontrols(False)
            ddlrbl.Visible = True
            irbl.Visible = True
            lblrbl.Text = "Select WareHouse Name."
            lblrbl.ForeColor = Drawing.Color.Black
        End If
    End Sub
    Public Sub fillother()
        ' fill combo with existing billing name
        ddlrbl.Items.Clear()
        Fill_Combo("Select BillingFrom_KId, BillingFrom_Name from BillingFrom where BillingFrom_UserId = '" & RemoveLiterals(Session("g_User").ToString.Trim) & "'and BillingFrom_Isdeleted='0'", ddlrbl)
        If ddlrbl.Items.Count <= 1 Then
            ' if records not found
            ddlrbl.Visible = False
            irbl.Visible = False
            lblrbl.Text = "No Records Found."
            lblrbl.ForeColor = Drawing.Color.Red
            enablecontrols(True)
        Else
            ' if record found
            enablecontrols(False)
            ddlrbl.Visible = True
            irbl.Visible = True
            lblrbl.Text = "Select WareHouse Name."
            lblrbl.ForeColor = Drawing.Color.Black
            ddlrbl.Items.Add("- Add New -")
        End If
    End Sub

    Protected Sub ddlrbl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        If ddlrbl.SelectedIndex > 0 Then
            If rblFrom.SelectedItem.Value = "F" Then
                ' if factory selected from rbl
                existingfactory(ddlrbl.SelectedItem.Value)
            ElseIf rblFrom.SelectedItem.Value = "W" Then
                ' if warehouse selecte from rbl
                existingwarehouse(ddlrbl.SelectedItem.Value)
            ElseIf rblFrom.SelectedItem.Value = "O" Then
                ' if other selected from rbl
                existingother(ddlrbl.SelectedItem.Value)
            End If
        Else
            clearfields()
            enablecontrols(False)
        End If
        
    End Sub
    Public Sub existingfactory(ByVal productid As String)
        Dim dt As DataTable = getTable("Select * from SupplierFactoryDetails where FactoryDetails_KId='" & RemoveLiterals(productid.ToString.Trim) & "' and FactoryDetails_IsDeleted='0' and FactoryDetails_CompanyID='" & Session("g_Company").ToString.Trim & "'", "SFD")
        If dt.Rows.Count > 0 Then
            ' get existing facotry details
            enablecontrols(True)
            txtFromType.Text = dt.Rows(0).Item("FactoryDetails_Relationship").ToString()
            txtAddress.Text = dt.Rows(0).Item("FactoryDetails_Address").ToString
            txtContactPerson.Text = dt.Rows(0).Item("FactoryDetails_ContactPerson").ToString
            txtEmailId.Text = dt.Rows(0).Item("FactoryDetails_EmailId").ToString
            txtFromName.Text = dt.Rows(0).Item("FactoryDetails_Name").ToString
            txtPersonDesignation.Text = dt.Rows(0).Item("FactoryDetails_PersonDesignation").ToString
            txtPhoneExtention.Text = dt.Rows(0).Item("FactoryDetails_PhoneExtn").ToString
            txtPinCode.Text = dt.Rows(0).Item("FactoryDetails_PinCode").ToString
            Dim pcountry1, pcity1, phoneno1, mcountry1, mobile1, fcountry1, fcity1, faxno1 As String

            Try
                phoneno1 = dt.Rows(0).Item("FactoryDetails_PhoneNo").ToString
                If phoneno1.Contains("-") Then
                    pcountry1 = phoneno1.Substring(0, phoneno1.IndexOf("-"))
                    phoneno1 = phoneno1.Substring(phoneno1.IndexOf("-") + 1)
                    If phoneno1.Contains("-") Then
                        pcity1 = phoneno1.Substring(0, phoneno1.IndexOf("-"))
                        phoneno1 = phoneno1.Substring(phoneno1.IndexOf("-") + 1)
                        txtCountryCode.Text = pcountry1
                        txtCityCode.Text = pcity1
                        txtPhoneNo.Text = phoneno1
                    Else
                        txtCityCode.Text = pcountry1
                        txtPhoneNo.Text = phoneno1
                    End If

                Else
                    txtPhoneNo.Text = phoneno1
                End If

            Catch ex As Exception
                txtCountryCode.Text = ""
                txtCityCode.Text = ""
                txtPhoneNo.Text = ""
            End Try

            Try
                mobile1 = dt.Rows(0).Item("FactoryDetails_MobileNo").ToString
                If mobile1.Contains("-") Then
                    mcountry1 = mobile1.Substring(0, mobile1.IndexOf("-"))
                    mobile1 = mobile1.Substring(mobile1.IndexOf("-") + 1)
                    txtCountryCodee.Text = mcountry1
                    txtMobileNo.Text = mobile1
                Else
                    txtMobileNo.Text = mobile1
                End If

            Catch ex As Exception
                txtCountryCodee.Text = ""
                txtMobileNo.Text = ""
            End Try

            Try
                If IsDBNull(dt.Rows(0).Item("FactoryDetails_FaxNo")) Then
                Else
                    faxno1 = dt.Rows(0).Item("FactoryDetails_FaxNo").ToString
                    If faxno1.Contains("-") Then
                        fcountry1 = faxno1.Substring(0, faxno1.IndexOf("-"))
                        faxno1 = faxno1.Substring(faxno1.IndexOf("-") + 1)
                        If faxno1.Contains("-") Then
                            fcity1 = faxno1.Substring(0, faxno1.IndexOf("-"))
                            faxno1 = faxno1.Substring(faxno1.IndexOf("-") + 1)
                            txtCountryCodeF.Text = fcountry1
                            txtCityCodeF.Text = fcity1
                            txtFaxNo.Text = faxno1
                        Else
                            txtCityCodeF.Text = fcountry1
                            txtFaxNo.Text = faxno1
                        End If

                    Else
                        txtFaxNo.Text = faxno1
                    End If

                End If
            Catch ex As Exception
                txtCountryCodeF.Text = ""
                txtCityCodeF.Text = ""
                txtFaxNo.Text = ""
            End Try

            Try
                If dt.Rows(0).Item("FactoryDetails_LocationId") = "" Then
                Else
                    getLocation(dt.Rows(0).Item("FactoryDetails_LocationId").ToString)
                End If
            Catch ex As Exception
                Fill_Combo("SELECT distinct Country_Kid, Country_Name FROM Country_Master where Country_IsDeleted='0'and Country_CompanyId='" & Session("g_Company").ToString & "' order by Country_Name", ddlCountry_Code)
                ddlCountry_Code.ClearSelection()
                ddlState_Code.Items.Clear()
                ddlCity_Code.Items.Clear()
            End Try

        End If
        dt.Clear() ' clear dt
    End Sub

    Private Sub getLocation(ByVal strLocID As String)
        ' get counryname, satatename and city name from StrLocID
        Dim strLocaId, strStateID, strCountry As String
        strLocaId = strLocID
        strStateID = ReturnValue("select City_StateId from City_Master where City_Kid='" & strLocaId.Trim & "'and City_IsDeleted='0' and City_CompanyID='" & Session("g_Company").ToString.Trim & "'")
        strCountry = ReturnValue("select State_CountryId from State_Master where State_Kid='" & strStateID.Trim & "'and State_IsDeleted='0' and State_CompanyID='" & Session("g_Company").ToString.Trim & "'")
        Fill_Combo("select Country_Kid,Country_Name from Country_Master where Country_IsDeleted='0' and Country_CompanyID='" & Session("g_Company").ToString.Trim & "' ", ddlCountry_Code)
        Fill_Combo("select State_Kid,State_Name from State_Master where State_CountryId='" & strCountry.Trim & "'and State_IsDeleted='0' and State_CompanyID='" & Session("g_Company").ToString.Trim & "'", ddlState_Code)
        Fill_Combo("select City_Kid,City_Name from City_Master where City_StateId='" & strStateID.Trim & "'and City_IsDeleted='0' and City_CompanyID='" & Session("g_Company").ToString.Trim & "' ", ddlCity_Code)
        ddlCountry_Code.ClearSelection()
        ddlCountry_Code.Items.FindByValue(strCountry.Trim).Selected = True

        ddlState_Code.ClearSelection()
        ddlState_Code.Items.FindByValue(strStateID.Trim).Selected = True

        ddlCity_Code.ClearSelection()
        ddlCity_Code.Items.FindByValue(strLocaId.Trim).Selected = True

    End Sub
    Public Sub existingwarehouse(ByVal productid As String)

        Dim dt As DataTable = getTable("Select * from StockingWarehouseInformation  where swi_KID='" & RemoveLiterals(productid.ToString.Trim) & "' and swi_IsDeleted='0' and swi_CompanyID='" & Session("g_Company").ToString.Trim & "'", "ASS")

        If dt.Rows.Count > 0 Then
            ' get existing warehouse details
            enablecontrols(True)
            txtFromType.Text = dt.Rows(0).Item("swi_whtype").ToString
            txtFromName.Text = dt.Rows(0).Item("swi_whname").ToString
            FillCombo()
            ddlCountry_Code.ClearSelection()
            Try
                ddlCountry_Code.Items.FindByText(ReturnValue("SELECT Country_Name FROM Country_Master WHERE Country_Kid='" & dt.Rows(0).Item("swi_country").ToString & "' AND Country_IsDeleted = '0'")).Selected = True
            Catch ex As Exception
            End Try
            ddlState_Code.ClearSelection()
            Try
                ddlState_Code.Items.FindByText(ReturnValue("SELECT State_Name FROM State_Master WHERE State_Kid='" & dt.Rows(0).Item("swi_state").ToString & "' AND State_IsDeleted = '0'")).Selected = True
            Catch ex As Exception
            End Try
            Fill_Combo("SELECT City_Kid, City_Name FROM City_Master WHERE City_StateID ='" & ddlState_Code.SelectedItem.Value & "'and City_CompanyId = '" & Session("g_Company").ToString & "' and City_IsDeleted = '0'", ddlCity_Code)
            ddlCity_Code.ClearSelection()
            Try
                ddlCity_Code.Items.FindByText(ReturnValue("SELECT City_Name FROM City_Master WHERE City_Kid='" & dt.Rows(0).Item("swi_city").ToString & "' AND City_IsDeleted = '0'")).Selected = True
            Catch ex As Exception
            End Try
            txtAddress.Text = dt.Rows(0).Item("swi_add").ToString
            txtPinCode.Text = dt.Rows(0).Item("swi_pinno").ToString

            txtCountryCode.Text = dt.Rows(0).Item("swi_phno1").ToString
            txtCityCode.Text = dt.Rows(0).Item("swi_phno2").ToString
            txtPhoneNo.Text = dt.Rows(0).Item("swi_phno3").ToString
            txtCountryCodeF.Text = dt.Rows(0).Item("swi_fax1").ToString
            txtCityCodeF.Text = dt.Rows(0).Item("swi_fax2").ToString
            txtFaxNo.Text = dt.Rows(0).Item("swi_fax3").ToString
            txtContactPerson.Text = dt.Rows(0).Item("swi_kcperson").ToString
            txtPersonDesignation.Text = dt.Rows(0).Item("swi_jtitle").ToString
            txtCountryCodee.Text = dt.Rows(0).Item("swi_mno1").ToString
            txtMobileNo.Text = dt.Rows(0).Item("swi_mno2").ToString
            txtEmailId.Text = dt.Rows(0).Item("swi_eid").ToString
        End If
        dt.Clear()

    End Sub
    Public Sub existingother(ByVal productid As String)

    End Sub

    Public Sub clearfields()
        txtAddress.Text = ""
        txtCity.Text = ""
        txtCityCode.Text = ""
        txtCityCodeF.Text = ""
        txtContactPerson.Text = ""
        txtCountryCode.Text = ""
        txtCountryCodee.Text = ""
        txtCountryCodeF.Text = ""
        txtEmailId.Text = ""
        txtFaxNo.Text = ""
        txtFromName.Text = ""
        txtFromType.Text = ""
        txtMobileNo.Text = ""
        txtPersonDesignation.Text = ""
        txtPhoneExtention.Text = ""
        txtPhoneNo.Text = ""
        txtPinCode.Text = ""
        ddlCity_Code.ClearSelection()
        ddlCountry_Code.ClearSelection()
        ddlState_Code.ClearSelection()

    End Sub
    Public Sub enablecontrols(ByVal action As Boolean)
        txtAddress.Enabled = action
        txtCity.Enabled = action
        txtCityCode.Enabled = action
        txtCityCodeF.Enabled = action
        txtContactPerson.Enabled = action
        txtCountryCode.Enabled = action
        txtCountryCodee.Enabled = action
        txtCountryCodeF.Enabled = action
        txtEmailId.Enabled = action
        txtFaxNo.Enabled = action
        txtFromName.Enabled = action
        txtFromType.Enabled = action
        txtMobileNo.Enabled = action
        txtPersonDesignation.Enabled = action
        txtPhoneExtention.Enabled = action
        txtPhoneNo.Enabled = action
        txtPinCode.Enabled = action
        ddlCity_Code.Enabled = action
        ddlCountry_Code.Enabled = action
        ddlState_Code.Enabled = action
        'rblFrom.Enabled = action
    End Sub

    
    Protected Sub ddlExisting_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If ddlExisting.SelectedIndex > 0 Then
            If ddlExisting.SelectedItem.Text = "Add New" Then
                ' if add new selected
                rblFrom.ClearSelection()
                rblFrom.SelectedValue = "O"
                clearfields()
                enablecontrols(True)
                FillCombo()
                rblFrom.Enabled = True
            Else
                ' existing product name seleted
                lblrbl.Visible = False
                ddlrbl.Visible = False
                Dim dt As DataTable
                dt = ReturnDataTable("SELECT Country_Master.Country_Name, Country_Master.Country_KID, BillingFrom.*, State_Master.State_Name, State_Master.State_KID, City_Master.City_Name, City_Master.City_KID, Product_Master.Product_Name FROM BillingFrom INNER JOIN City_Master ON BillingFrom.BillingFrom_LocationId = City_Master.City_Kid INNER JOIN State_Master ON City_Master.City_StateId = State_Master.State_Kid INNER JOIN Country_Master ON State_Master.State_CountryId = Country_Master.Country_Kid INNER JOIN Product_Master ON BillingFrom.BillingFrom_ProductID = Product_Master.Product_Kid WHERE (BillingFrom.BillingFrom_ProductID = '" & RemoveLiterals(ddlExisting.SelectedItem.Value.ToString.Trim) & "') AND (BillingFrom.BillingFrom_IsDeleted = '0')")
                rblFrom.Enabled = False
                enablecontrols(False)
                rblFrom.ClearSelection()
                Try
                    rblFrom.SelectedValue = dt.Rows(0).Item("BillingFrom_From").ToString()
                Catch ex As Exception
                    rblFrom.SelectedValue = "O"
                End Try
                txtFromType.Text = dt.Rows(0).Item("BillingFrom_type").ToString
                txtFromName.Text = dt.Rows(0).Item("BillingFrom_Name").ToString
                FillCombo()
                ddlCountry_Code.ClearSelection()
                Try
                    ddlCountry_Code.Items.FindByValue(dt.Rows(0).Item("Country_Kid")).Selected = True
                Catch ex As Exception
                End Try
                ddlState_Code.ClearSelection()
                Try
                    ddlState_Code.Items.FindByValue(dt.Rows(0).Item("State_Kid")).Selected = True
                Catch ex As Exception
                End Try
                Fill_Combo("SELECT City_Kid, City_Name FROM City_Master WHERE City_StateID ='" & ddlState_Code.SelectedItem.Value & "'and City_CompanyId = '" & Session("g_Company").ToString & "' and City_IsDeleted = '0'", ddlCity_Code)
                ddlCity_Code.ClearSelection()
                Try
                    ddlCity_Code.Items.FindByValue(dt.Rows(0).Item("City_Kid")).Selected = True
                Catch ex As Exception
                End Try
                txtAddress.Text = dt.Rows(0).Item("BillingFrom_Address").ToString
                txtPinCode.Text = dt.Rows(0).Item("BillingFrom_PinCode").ToString

                Dim pcountry1, pcity1, phoneno1, mcountry1, mobile1, fcountry1, fcity1, faxno1 As String

                Try
                    phoneno1 = dt.Rows(0).Item("BillingFrom_PhoneNo").ToString
                    If phoneno1.Contains("-") Then
                        pcountry1 = phoneno1.Substring(0, phoneno1.IndexOf("-"))
                        phoneno1 = phoneno1.Substring(phoneno1.IndexOf("-") + 1)
                        If phoneno1.Contains("-") Then
                            pcity1 = phoneno1.Substring(0, phoneno1.IndexOf("-"))
                            phoneno1 = phoneno1.Substring(phoneno1.IndexOf("-") + 1)
                            txtCountryCode.Text = pcountry1
                            txtCityCode.Text = pcity1
                            txtPhoneNo.Text = phoneno1
                        Else
                            txtCityCode.Text = pcountry1
                            txtPhoneNo.Text = phoneno1
                        End If

                    Else
                        txtPhoneNo.Text = phoneno1
                    End If

                Catch ex As Exception
                    txtCountryCode.Text = ""
                    txtCityCode.Text = ""
                    txtPhoneNo.Text = ""
                End Try

                Try
                    mobile1 = dt.Rows(0).Item("BillingFrom_MobileNo").ToString
                    If mobile1.Contains("-") Then
                        mcountry1 = mobile1.Substring(0, mobile1.IndexOf("-"))
                        mobile1 = mobile1.Substring(mobile1.IndexOf("-") + 1)
                        txtCountryCodee.Text = mcountry1
                        txtMobileNo.Text = mobile1
                    Else
                        txtMobileNo.Text = mobile1
                    End If

                Catch ex As Exception
                    txtCountryCodee.Text = ""
                    txtMobileNo.Text = ""
                End Try

                Try
                    If IsDBNull(dt.Rows(0).Item("BillingFrom_FaxNo")) Then
                    Else
                        faxno1 = dt.Rows(0).Item("BillingFrom_FaxNo").ToString
                        If faxno1.Contains("-") Then
                            fcountry1 = faxno1.Substring(0, faxno1.IndexOf("-"))
                            faxno1 = faxno1.Substring(faxno1.IndexOf("-") + 1)
                            If faxno1.Contains("-") Then
                                fcity1 = faxno1.Substring(0, faxno1.IndexOf("-"))
                                faxno1 = faxno1.Substring(faxno1.IndexOf("-") + 1)
                                txtCountryCodeF.Text = fcountry1
                                txtCityCodeF.Text = fcity1
                                txtFaxNo.Text = faxno1
                            Else
                                txtCityCodeF.Text = fcountry1
                                txtFaxNo.Text = faxno1
                            End If

                        Else
                            txtFaxNo.Text = faxno1
                        End If

                    End If
                Catch ex As Exception
                    txtCountryCodeF.Text = ""
                    txtCityCodeF.Text = ""
                    txtFaxNo.Text = ""
                End Try

                txtContactPerson.Text = dt.Rows(0).Item("BillingFrom_ContactPerson").ToString
                txtPersonDesignation.Text = dt.Rows(0).Item("BillingFrom_PersonDesignation").ToString
                txtEmailId.Text = dt.Rows(0).Item("BillingFrom_EmailID").ToString
                txtPhoneExtention.Text = dt.Rows(0).Item("BillingFrom_PhoneExtn").ToString()
                dt.Clear()
            End If
        Else
            lblrbl.Visible = False
            ddlrbl.Visible = False
            rblFrom.ClearSelection()
            rblFrom.SelectedValue = "O"
            rblFrom.Enabled = False
            clearfields()
            enablecontrols(False)
        End If
    End Sub
End Class
