Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports Ajax
Partial Class client_master
    Inherits System.Web.UI.Page
    Shared clientcode As String
    Shared img As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Ajax.Utility.RegisterTypeForAjax(GetType(client_master))
        Try
            If Session("SupplierID") Is Nothing And Session("AdminID") = Nothing Then
                Response.Redirect("listproduct.aspx?sess=expire")
                Exit Sub
            End If
            If (Not IsPostBack) Then
                img = 0
                lnkFile.Visible = False
                txt_name.Focus()
                btnADD.Focus()

                If Session("SupplierID") Is Nothing Then ' check whether supplier is loged in or not
                    Response.Redirect("listproduct.aspx")
                    Exit Sub
                End If

                btnDelete.Attributes.Add("onclick", "return ConfrmMsg();")
                btnUpdate.Attributes.Add("onclick", "return UpdateMsg();")
                btnSave.Attributes.Add("onclick", "return SaveConfirm();")

                txt_abusiness.Attributes.Add("onkeypress", "return numbersonly(event);")
                txt_phone1.Attributes.Add("onkeypress", "return numbersonly(event);")
                txt_phone2.Attributes.Add("onkeypress", "return numbersonly(event);")
                txt_phone3.Attributes.Add("onkeypress", "return numbersonly(event);")
                txt_phone4.Attributes.Add("onkeypress", "return numbersonly(event);")
                txt_mobile1.Attributes.Add("onkeypress", "return numbersonly(event);")
                txt_mobile2.Attributes.Add("onkeypress", "return numbersonly(event);")

                If Request.QueryString("BackId") = Nothing Then
                    If Session("Index") <> Nothing Then
                        btnBack.Visible = True
                    Else
                        Session("ProductID") = Nothing   ' Sessioon productid set nothing
                        btnBack.Visible = False
                    End If
                Else
                    Session("ProductID") = Nothing
                    Session("Index") = Nothing
                    btnBack.Visible = False
                End If


                ClearFields()               'Clear the TextFields
                clientcode = ""
                rblYesNo.SelectedIndex = 1
                'file.Visible = False
                unLockControl(False)
                Try
                    Fill_Combo("SELECT    Country_Kid, Country_Name FROM Country_Master WHERE     Country_IsDeleted = N'0' AND Country_CompanyId = '" & Session("g_Company") & "'", ddl_country)
                    ddl_country.Items.FindByText("India").Selected = True
                Catch
                End Try
                ControlFill()

                'btnOther.Visible = False
                getTableData()

                CancelClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
                Call CheckRights(Me, Session("g_User").ToString.Trim, "client-master")
                ' call cancel fuction for contact

                If Session("AdminID") <> Nothing Or Session("AdminID") = Nothing Then
                    btnUpdate.Visible = False
                    btnDelete.Visible = False
                Else
                    btnUpdate.Visible = False
                    btnDelete.Visible = False
                End If
                file.Style.Add("display", "none")
                rblYesNo.Attributes.Add("onclick", "return rblProductMSDSClick(this);")
                ddl_city.Enabled = False
            End If
            btnADD.Focus()

        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    <Ajax.AjaxMethod()> _
  Public Function AddCity(ByVal strCity As String, ByVal strStateID As String, ByVal strCompanyID As String, ByVal strUserID As String, ByVal strFinancialYearID As String) As String
        Dim KID As String
        KID = NewPrimaryKey(Me)

        If IsAlreadyPresent("Select count(*) from City_Master where City_Name='" & strCity.Trim & "' and City_StateId ='" & strStateID.ToString & "'") Then
            Return "false"
        End If

        Dim con As New SqlConnection()
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")        'Read the connection string from web.config
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "City_Master_Proc"
        cmd.Connection = con
        cmd.Parameters.Add(New SqlParameter("@City_Kid", SqlDbType.NVarChar, 10)).Value = KID
        cmd.Parameters.Add(New SqlParameter("@City_Code", SqlDbType.NVarChar, 25)).Value = Generate_Code("Select count(*) from City_Master where City_Code='", "CC").ToString()
        cmd.Parameters.Add(New SqlParameter("@City_Name", SqlDbType.NVarChar, 100)).Value = strCity.Trim
        cmd.Parameters.Add(New SqlParameter("@City_StateId", SqlDbType.NVarChar, 10)).Value = strStateID.Trim
        cmd.Parameters.Add(New SqlParameter("@City_CompanyId", SqlDbType.NVarChar, 10)).Value = Trim(strCompanyID)
        cmd.Parameters.Add(New SqlParameter("@City_UserId", SqlDbType.NVarChar, 10)).Value = Trim(strUserID)
        cmd.Parameters.Add(New SqlParameter("@City_FinancialYearId", SqlDbType.NVarChar, 10)).Value = strFinancialYearID.Trim
        cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "SupplierRegistration1"
        cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 10)).Value = "Insert"
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            Return KID
        Catch ex As Exception
            con.Close()
            Return "false"
        End Try
        Return True

    End Function
    'Public Sub getGridTable()
    '    Dim dt As New DataTable
    '    dt = ReturnDataTable("SELECT    Client_Master.ctm_kid, Client_Master.ctm_name, Client_Master.ctm_abusiness, client_master.ctm_rcontact1, client_master.ctm_rcontact2, client_master.ctm_emailid, client_master.ctm_ctp, client_master.ctm_ctpDoc, client_master.ctm_city FROM Client_Master WHERE     ctm_SupplierId = '" & Session("SupplierID") & "' AND ctm_companyid = '" & Session("g_Company") & "' AND ctm_IsDeleted = N'0'")

    '    If dt.Rows.Count > 0 Then
    '        titlegv.Visible = True
    '        gvtable.Visible = True
    '        gvClienteleDetail.Visible = True
    '        gvClienteleDetail.DataSource = dt
    '        gvClienteleDetail.DataBind()
    '        getTableData()
    '    Else
    '        titlegv.Visible = False
    '        gvtable.Visible = False
    '        gvClienteleDetail.Visible = False
    '    End If
    '  End Sub


    Public Sub getTableData()
        Try
            Dim dt As DataTable = ReturnDataTable("SELECT  Client_Master.ctm_kid, Client_Master.ctm_name, Client_Master.ctm_abusiness, client_master.ctm_rcontact1, client_master.ctm_rcontact2 , client_master.ctm_emailid, client_master.ctm_ctp, client_master.ctm_ctpDoc,client_master.ctm_city FROM Client_Master WHERE ctm_SupplierId = '" & Session("SupplierID") & "' AND ctm_companyid = '" & Session("g_Company") & "' AND ctm_IsDeleted = N'0'")
            If dt.Rows.Count > 0 Then
                titlegv.Visible = True
                gvtable.Visible = True
                gvClienteleDetail.Visible = True
                gvClienteleDetail.DataSource = dt
                gvClienteleDetail.DataBind()
                gvClienteleDetail.SelectedIndex = -1
                gvClienteleDetail.PageIndex = 0

                Dim i As Integer
                If Session("AdminID") <> Nothing Or Session("AdminID") = Nothing Then
                    gvClienteleDetail.Columns(0).Visible = False
                Else
                    gvClienteleDetail.Columns(0).Visible = False
                End If
                For i = 0 To gvClienteleDetail.Rows.Count - 1
                    Dim lblreference As Label = CType(gvClienteleDetail.Rows(i).Cells(4).FindControl("lblgrvRefernece"), Label)
                    'If dt.Rows(i).Item("ctm_rcontact1").ToString = "0" Then
                    lblreference.Text = dt.Rows(i).Item("ctm_rcontact1").ToString
                    'ElseIf dt.Rows(i).Item("ctm_rcontact1").ToString = "1" Then
                    ' lblreference.Text = "Ms."
                    'End If
                    lblreference.Text &= " " & dt.Rows(i).Item("ctm_rcontact2")
                    ' Dim lblcity As Label = CType(gvClienteleDetail.Rows(i).Cells(7).FindControl("lnkDoc"), Label)
                    Dim lblDoc As HyperLink = CType(gvClienteleDetail.Rows(i).Cells(7).FindControl("lnkDoc"), HyperLink)
                    lblDoc.Text = dt.Rows(i).Item("ctm_ctpDoc").ToString
                    lblDoc.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(i).Item("ctm_ctpDoc").ToString

                    Dim lblCity As Label = CType(gvClienteleDetail.Rows(i).Cells(2).FindControl("lblgvCity"), Label)
                    lblCity.Text = ReturnValue("select City_Name from city_master where city_kid='" & dt.Rows(i).Item("ctm_city").ToString & "'").ToString

                    Dim lblYes As Label = CType(gvClienteleDetail.Rows(i).Cells(7).FindControl("lblYesNo"), Label)
                    If dt.Rows(i).Item("ctm_ctp").ToString = "Y" Then
                        lblYes.Text = "Yes"
                    Else
                        lblYes.Text = "No"
                    End If

                Next

            Else
                titlegv.Visible = False
                gvtable.Visible = False
                gvClienteleDetail.Visible = False
                ' btnSave.Visible = True
                ' btnAddMore.Visible = True
            End If

            dt.Clear()
        Catch
        End Try

    End Sub

    Public Sub unLockControl(ByVal bool As Boolean)
        Try
            txt_name.Enabled = bool

            txt_abusiness.Enabled = bool
            txt_email.Enabled = bool
            txt_jtitle.Enabled = bool
            txt_mobile1.Enabled = bool
            txt_mobile2.Enabled = bool
            '  txt_name.Enabled = bool
            txt_phone1.Enabled = bool
            txt_phone2.Enabled = bool
            txt_phone3.Enabled = bool
            txt_phone4.Enabled = bool
            txt_rcontact.Enabled = bool
            ddl_city.Enabled = bool
            ddl_country.Enabled = bool
            ddl_rcontact.Enabled = bool
            ddl_state.Enabled = bool
            rblYesNo.Enabled = bool
        Catch
        End Try
    End Sub

    Protected Sub ClearFields()
        Try
            txt_abusiness.Text = ""                           'Assign Null to the TextField

            txt_email.Text = ""                           'Assign Null to the TextField
            txt_jtitle.Text = ""                           'Assign Null to the TextField
            txt_mobile1.Text = ""                           'Assign Null to the TextField
            txt_mobile2.Text = ""                           'Assign Null to the TextField
            txt_name.Text = ""                           'Assign Null to the TextField
            txt_phone1.Text = ""                           'Assign Null to the TextField 
            txt_phone2.Text = ""                           'Assign Null to the TextField
            txt_phone3.Text = ""                           'Assign Null to the TextField
            txt_rcontact.Text = ""                           'Assign Null to the TextField
            txt_phone4.Text = ""                           'Assign Null to the TextField
            ddl_city.SelectedIndex = 0                           'Assign Null to the TextField
            'ddl_country.SelectedIndex = 0
            ddl_rcontact.SelectedIndex = 0
            ddl_state.SelectedIndex = 0

        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Public Sub ControlFill()
        Try
            ' ddl_country.ClearSelection()
            'ddl_country.Items.FindByText("India").Selected = True
            ddl_country_SelectedIndexChanged(Nothing, Nothing)
        Catch
        End Try
        Try
            'ddl_state.ClearSelection()
            ' ddl_state.Items.FindByText("Maharashtra").Selected = True
            ddl_state_SelectedIndexChanged(Nothing, Nothing)
        Catch
        End Try
        Try
            'ddl_city.ClearSelection()
            'ddl_city.Items.FindByText("Mumbai").Selected = True
        Catch
        End Try
        Try
            txt_phone1.Text = ""
            txt_phone1.Text = ReturnValue("SELECT Country_ISDCode FROM Country_Master WHERE Country_Kid = '" & ddl_country.SelectedValue.ToString.Trim & "'")
        Catch
        End Try
        Try
            txt_mobile1.Text = ReturnValue("SELECT Country_ISDCode FROM Country_Master WHERE Country_Kid = '" & ddl_country.SelectedValue.ToString.Trim & "'")
        Catch
        End Try
    End Sub

    Protected Sub ddl_country_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_country.SelectedIndexChanged
        Try
            If ddl_country.SelectedIndex > 0 Then
                Dim cCode As String = ReturnValue("SELECT Country_ISDCode FROM Country_Master WHERE Country_Kid = '" & ddl_country.SelectedValue.ToString.Trim & "'")
                txt_phone1.Text = ""
                txt_phone1.Text = cCode.ToString
                Fill_Combo("SELECT State_Kid, State_Name FROM State_Master WHERE  State_IsDeleted = N'0' AND State_CompanyId = '" & Session("g_Company") & "' and  State_CountryId='" & ddl_country.SelectedValue & "'", ddl_state)
                'ScriptManager.RegisterClientScriptBlock(sender, sender.GetType(), "MyKey", "WebForm_AutoFocus('" + ddl_state.ClientID + "');", True)
                ddl_state.ClearSelection()
                ddl_city.Items.Clear()
            End If
        Catch
        End Try
    End Sub

    Protected Sub ddl_state_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_state.SelectedIndexChanged
        Try
            If ddl_country.SelectedIndex > 0 Then
                ddl_city.Items.Clear()
                'Fill_Combo("SELECT     City_Kid, City_Name     FROM City_Master WHERE     City_IsDeleted = N'0' AND City_CompanyId = '" & Session("g_Company") & "'  and City_StateId='" & ddl_state.SelectedValue & "'", ddl_city)
                Fill_Combo("SELECT     City_Kid, City_Name     FROM City_Master WHERE     City_IsDeleted = N'0' AND City_CompanyId = '" & Session("g_Company") & "'  and City_StateId='" & ddl_state.SelectedValue & "' order by City_Name", ddl_city)
                'ScriptManager.RegisterClientScriptBlock(sender, sender.GetType(), "MyKey", "WebForm_AutoFocus('" + ddl_city.ClientID + "');", True)
                ddl_city.Enabled = True
                ' ddl_city.Attributes.Add("onchange", "return checkCityddl(this,'" & ddl_state.SelectedItem.Value.ToString & "','" & Session("g_Company").ToString & "','" & Session("g_User") & "','" & Session("g_FinYear") & "');")
            End If
        Catch
        End Try
    End Sub
    'Protected Sub ddl_city_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_city.SelectedIndexChanged
    '    Try
    '        If ddl_city.SelectedIndex > 0 Then
    '            If ddl_city.SelectedItem.Text = "Other" Then
    '                txt_city.Visible = True
    '                'btnOther.Visible = True
    '            End If
    '        End If
    '    Catch
    '    End Try
    'End Sub

    Protected Sub btnADD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnADD.Click
        ScriptManager1.SetFocus(txt_name)
        getTableData()
        unLockControl(True)
        ddl_country.Items.FindByText("India").Selected = True
        ClearFields()
        AddClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
    End Sub
    Protected Sub GenerateClienteleDetailsCode(ByRef code As String)
        Try
            Dim i As String
            Dim qry As String
            'Dim code As String
            ' txtProductReviewKid.Text = NewPrimaryKey(Me)
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "CDC" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from Client_Master where ctm_code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    '  txtProductReviewCode.Text = 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 btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            'If ddl_city.SelectedIndex = 0 Then                                         'If txtPartTypeName TextField is Null then
            '    CreateMessageAlert(Me, "Select a Proper City !", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            'If ddl_country.SelectedIndex = 0 Then                                         'If txtPartTypeName TextField is Null then
            '    CreateMessageAlert(Me, "Select a Proper Country !", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            'If ddl_state.SelectedIndex = 0 Then                                         'If txtPartTypeName TextField is Null then
            '    CreateMessageAlert(Me, "Select a Proper State !", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            Dim filename As String = ""
            'Dim filepath As String

            If FileUpload1.HasFile Then
                filename = FileUpload1.FileName
                If Path.GetExtension(FileUpload1.FileName).ToString.ToLower = ".pdf" Then
                    filename = Final_FileUpload(FileUpload1, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))
                    'FileUpload1.SaveAs(MapPath(filepath))
                End If
            End If

            GenerateClienteleDetailsCode(clientcode)
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Client_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ctm_kid", SqlDbType.NVarChar, 10)).Value = Trim(NewPrimaryKey(Me))       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_code", SqlDbType.NVarChar, 25)).Value = Trim(clientcode)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_SupplierId", SqlDbType.NVarChar, 10)).Value = Session("SupplierID")   'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_name", SqlDbType.NVarChar, 100)).Value = txt_name.Text.Trim
            cmd.Parameters.Add(New SqlParameter("@ctm_country", SqlDbType.NVarChar, 10)).Value = Trim(ddl_country.SelectedValue)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_state", SqlDbType.NVarChar, 10)).Value = Trim(ddl_state.SelectedValue)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_city", SqlDbType.NVarChar, 10)).Value = Trim(ddl_city.SelectedValue)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            If txt_abusiness.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_abusiness", SqlDbType.Decimal, 10)).Value = Convert.ToDecimal(txt_abusiness.Text.Trim)
            End If

            cmd.Parameters.Add(New SqlParameter("@ctm_rcontact1", SqlDbType.NVarChar, 10)).Value = Trim(ddl_rcontact.SelectedValue)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_rcontact2", SqlDbType.NVarChar, 25)).Value = Trim(txt_rcontact.Text)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_jtitle", SqlDbType.NVarChar, 50)).Value = Trim(txt_jtitle.Text)
            cmd.Parameters.Add(New SqlParameter("@ctm_pcountrycode", SqlDbType.NVarChar, 4)).Value = Trim(txt_phone1.Text)       'Add Parameter Banke_Kid and assign the value  to it
            If txt_phone2.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_pcitycode", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone2.Text))       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            End If
            If txt_phone3.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_phone", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone3.Text))      'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If

            If txt_phone4.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_pextn", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone4.Text)) 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If
            cmd.Parameters.Add(New SqlParameter("@ctm_emailid", SqlDbType.NVarChar, 50)).Value = Trim(txt_email.Text)
            cmd.Parameters.Add(New SqlParameter("@ctm_mcountrycode", SqlDbType.NVarChar, 4)).Value = Trim(txt_mobile1.Text)       'Add Parameter Banke_Kid and assign the value  to it
            If txt_mobile2.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_mphone", SqlDbType.Decimal, 18)).Value = Convert.ToDecimal(Trim(txt_mobile2.Text))       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            End If

            cmd.Parameters.Add(New SqlParameter("@ctm_ctp", SqlDbType.NVarChar, 1)).Value = Trim(rblYesNo.SelectedValue)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            If FileUpload1.FileName <> Nothing Then
                cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = Trim(filename) 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            Else
                cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = ""  'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If
            cmd.Parameters.Add(New SqlParameter("@ctm_companyid", SqlDbType.NVarChar, 10)).Value = Trim(Session("g_Company"))            'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@ctm_userid", SqlDbType.NVarChar, 10)).Value = Trim(Session("g_User").ToString.Trim)
            cmd.Parameters.Add(New SqlParameter("@ctm_financialyearid", SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString          'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "client-master"                           'Add Parameter FormName and assign the name of form to it
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Insert"                                   'Add Parameter Mode and assign the DML Operation to Execute (Insert)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()                                                                 'Fill the Grid
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            getTableData()
            gvClienteleDetail.SelectedIndex = -1
            ScriptManager1.SetFocus(btnADD)
            unLockControl(False)
            SaveClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub rblYesNo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblYesNo.SelectedIndexChanged
        Try
            If rblYesNo.SelectedValue = "N" Then
                'file.Visible = False
            Else
                ' file.Visible = True
            End If
        Catch
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        gvClienteleDetail.SelectedIndex = -1
        ScriptManager1.SetFocus(btnADD)
        unLockControl(False)
        ClearFields()
        lnkFile.Visible = False
        CancelClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
    End Sub

    Protected Sub gvClienteleDetail_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvClienteleDetail.SelectedIndexChanged
        Dim dt As DataTable = ReturnDataTable("SELECT  Client_Master.* FROM Client_Master  WHERE ctm_kid='" & RemoveLiterals(gvClienteleDetail.SelectedDataKey.Value.ToString) & "' and ctm_IsDeleted='0'")

        If dt.Rows.Count > 0 Then
            Try
                txt_abusiness.Text = dt.Rows(0).Item("ctm_abusiness").ToString
                txt_email.Text = dt.Rows(0).Item("ctm_emailid").ToString
                txt_jtitle.Text = dt.Rows(0).Item("ctm_jtitle").ToString
                txt_mobile1.Text = dt.Rows(0).Item("ctm_mcountrycode").ToString
                txt_mobile2.Text = dt.Rows(0).Item("ctm_mphone").ToString
                txt_name.Text = dt.Rows(0).Item("ctm_name").ToString
                txt_phone1.Text = dt.Rows(0).Item("ctm_pcountrycode").ToString
                txt_phone2.Text = dt.Rows(0).Item("ctm_pcitycode").ToString
                txt_phone3.Text = dt.Rows(0).Item("ctm_phone").ToString
                If IsDBNull(dt.Rows(0).Item("ctm_pextn")) Then
                    txt_phone4.Text = ""
                Else
                    txt_phone4.Text = dt.Rows(0).Item("ctm_pextn").ToString
                End If
                txt_rcontact.Text = dt.Rows(0).Item("ctm_rcontact2").ToString
                Try
                    FillComboWithOthers("SELECT     City_Kid, City_Name     FROM City_Master WHERE     City_IsDeleted = N'0' AND City_CompanyId = '" & Session("g_Company") & "'  ", ddl_city)
                    ddl_city.SelectedIndex = ddl_city.Items.IndexOf(ddl_city.Items.FindByValue(dt.Rows(0).Item("ctm_city").ToString))
                Catch
                End Try
                'ddl_city.SelectedValue = dt.Rows(0).Item("ctm_city").ToString
                Try
                    'ddl_country.SelectedIndex = ddl_country.Items.IndexOf(ddl_country.Items.FindByValue(dt.Rows(0).Item("ctm_country").ToString))
                    ddl_country.Items.FindByText("India").Selected = True
                Catch
                End Try
                Try
                    ddl_rcontact.SelectedIndex = ddl_rcontact.Items.IndexOf(ddl_rcontact.Items.FindByValue(dt.Rows(0).Item("ctm_rcontact1").ToString))
                Catch
                End Try
                Try
                    ddl_state.SelectedIndex = ddl_state.Items.IndexOf(ddl_state.Items.FindByValue(dt.Rows(0).Item("ctm_state").ToString))
                Catch
                End Try
                Try
                    rblYesNo.SelectedValue = dt.Rows(0).Item("ctm_ctp").ToString
                    If dt.Rows(0).Item("ctm_ctp").ToString = "Y" Then

                        file.Style.Add("display", "auto")
                        lnkFile.Text = dt.Rows(0).Item("ctm_ctpDoc").ToString
                        If lnkFile.Text <> "" Then
                            lnkFile.Visible = True
                            lnkFile.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & lnkFile.Text
                        Else
                            lnkFile.Visible = True
                            lnkFile.Text = "No File is Uploaded"
                        End If
                    Else
                        file.Style.Add("display", "none")
                    End If

                Catch
                End Try

                Try

                Catch
                End Try
                FileUpload1.Visible = True
                btnSave.Visible = False

                If Session("AdminID") <> Nothing Or Session("AdminID") = Nothing Then
                    btnDelete.Visible = True
                    btnUpdate.Visible = True
                    btnADD.Visible = False
                    unLockControl(True)
                Else
                    btnDelete.Visible = False
                    btnUpdate.Visible = False
                    btnADD.Visible = True
                    unLockControl(False)
                End If
                btnCancel.Visible = True

            Catch ex As Exception

            End Try
        End If

    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            Dim dt As DataTable = getTable("SELECT  * FROM Client_Master  WHERE ctm_kid='" & RemoveLiterals(gvClienteleDetail.SelectedDataKey.Value.ToString) & "'", "PR")

            Dim filename As String = ""
            'Dim filepath As String
            If FileUpload1.HasFile Then
                filename = FileUpload1.FileName
                If Path.GetExtension(FileUpload1.FileName).ToString.ToLower = ".pdf" Then
                    filename = Final_FileUpload(FileUpload1, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))
                    'FileUpload1.SaveAs(MapPath(filepath))
                End If
            End If

            GenerateClienteleDetailsCode(clientcode)
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Client_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ctm_kid", SqlDbType.NVarChar, 10)).Value = RemoveLiterals(dt.Rows(0).Item("ctm_kid").ToString.Trim)      'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_code", SqlDbType.NVarChar, 25)).Value = RemoveLiterals(dt.Rows(0).Item("ctm_code").ToString.Trim)      'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_SupplierId", SqlDbType.NVarChar, 10)).Value = Session("SupplierID")   'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_name", SqlDbType.NVarChar, 100)).Value = txt_name.Text.Trim
            cmd.Parameters.Add(New SqlParameter("@ctm_country", SqlDbType.NVarChar, 10)).Value = Trim(ddl_country.SelectedValue)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_state", SqlDbType.NVarChar, 10)).Value = Trim(ddl_state.SelectedValue)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_city", SqlDbType.NVarChar, 10)).Value = Trim(ddl_city.SelectedValue)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            If txt_abusiness.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_abusiness", SqlDbType.Decimal, 10)).Value = Convert.ToDecimal(txt_abusiness.Text.Trim)
            End If
            cmd.Parameters.Add(New SqlParameter("@ctm_rcontact1", SqlDbType.NVarChar, 10)).Value = Trim(ddl_rcontact.SelectedValue)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_rcontact2", SqlDbType.NVarChar, 25)).Value = Trim(txt_rcontact.Text)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_jtitle", SqlDbType.NVarChar, 50)).Value = Trim(txt_jtitle.Text)
            cmd.Parameters.Add(New SqlParameter("@ctm_pcountrycode", SqlDbType.NVarChar, 4)).Value = Trim(txt_phone1.Text)       'Add Parameter Banke_Kid and assign the value  to it
            If txt_phone2.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_pcitycode", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone2.Text))       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            End If
            If txt_phone3.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_phone", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone3.Text))      'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If

            If txt_phone4.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_pextn", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone4.Text)) 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If
            cmd.Parameters.Add(New SqlParameter("@ctm_emailid", SqlDbType.NVarChar, 50)).Value = Trim(txt_email.Text)
            cmd.Parameters.Add(New SqlParameter("@ctm_mcountrycode", SqlDbType.NVarChar, 4)).Value = Trim(txt_mobile1.Text)       'Add Parameter Banke_Kid and assign the value  to it
            If txt_mobile2.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_mphone", SqlDbType.Decimal, 18)).Value = Convert.ToDecimal(Trim(txt_mobile2.Text))       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            End If

            cmd.Parameters.Add(New SqlParameter("@ctm_ctp", SqlDbType.NVarChar, 1)).Value = Trim(rblYesNo.SelectedValue)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            If rblYesNo.SelectedValue = "N" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = ""
            Else
                If FileUpload1.FileName <> Nothing Then
                    cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = Trim(filename) 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
                Else
                    cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = dt.Rows(0).Item("ctm_ctpDoc").ToString.Trim 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
                End If
            End If

            cmd.Parameters.Add(New SqlParameter("@ctm_companyid", SqlDbType.NVarChar, 10)).Value = Trim(Session("g_Company"))            'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@ctm_userid", SqlDbType.NVarChar, 10)).Value = Trim(Session("g_User").ToString.Trim)
            cmd.Parameters.Add(New SqlParameter("@ctm_financialyearid", SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString          'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "client-master"                           'Add Parameter FormName and assign the name of form to it
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Update"                                   'Add Parameter Mode and assign the DML Operation to Execute (Insert)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()                                                                 'Fill the Grid
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            getTableData()
            EditClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
            dt.Clear()
            gvClienteleDetail.SelectedIndex = -1
            ScriptManager1.SetFocus(btnADD)
            unLockControl(False)
            lnkFile.Visible = False
            EditClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Dim dt As DataTable = getTable("SELECT  * FROM Client_Master  WHERE ctm_kid='" & RemoveLiterals(gvClienteleDetail.SelectedDataKey.Value.ToString) & "'", "PR")

            Dim filename As String = ""
            'Dim filepath As String
            If FileUpload1.HasFile Then
                filename = FileUpload1.FileName
                If Path.GetExtension(FileUpload1.FileName).ToString.ToLower = ".pdf" Then
                    filename = Final_FileUpload(FileUpload1, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))
                    'FileUpload1.SaveAs(MapPath(filepath))
                End If
            End If

            GenerateClienteleDetailsCode(clientcode)
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Client_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ctm_kid", SqlDbType.NVarChar, 10)).Value = RemoveLiterals(dt.Rows(0).Item("ctm_kid").ToString.Trim)      'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_code", SqlDbType.NVarChar, 25)).Value = RemoveLiterals(dt.Rows(0).Item("ctm_code").ToString.Trim)      'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_SupplierId", SqlDbType.NVarChar, 10)).Value = Session("SupplierID")   'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_name", SqlDbType.NVarChar, 100)).Value = txt_name.Text.Trim
            cmd.Parameters.Add(New SqlParameter("@ctm_country", SqlDbType.NVarChar, 10)).Value = Trim(ddl_country.SelectedValue)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_state", SqlDbType.NVarChar, 10)).Value = Trim(ddl_state.SelectedValue)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_city", SqlDbType.NVarChar, 10)).Value = Trim(ddl_city.SelectedValue)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            If txt_abusiness.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_abusiness", SqlDbType.Decimal, 10)).Value = Convert.ToDecimal(txt_abusiness.Text.Trim)
            End If
            cmd.Parameters.Add(New SqlParameter("@ctm_rcontact1", SqlDbType.NVarChar, 10)).Value = Trim(ddl_rcontact.SelectedValue)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ctm_rcontact2", SqlDbType.NVarChar, 25)).Value = Trim(txt_rcontact.Text)       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ctm_jtitle", SqlDbType.NVarChar, 50)).Value = Trim(txt_jtitle.Text)
            cmd.Parameters.Add(New SqlParameter("@ctm_pcountrycode", SqlDbType.NVarChar, 4)).Value = Trim(txt_phone1.Text)       'Add Parameter Banke_Kid and assign the value  to it

            If txt_phone2.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_pcitycode", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone2.Text))       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            End If
            If txt_phone3.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_phone", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone3.Text))      'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If

            If txt_phone4.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_pextn", SqlDbType.Decimal, 18)).Value = Convert.ToInt32(Trim(txt_phone4.Text)) 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            End If
            cmd.Parameters.Add(New SqlParameter("@ctm_emailid", SqlDbType.NVarChar, 50)).Value = Trim(txt_email.Text)
            cmd.Parameters.Add(New SqlParameter("@ctm_mcountrycode", SqlDbType.NVarChar, 4)).Value = Trim(txt_mobile1.Text)       'Add Parameter Banke_Kid and assign the value  to it
            If Trim(txt_mobile2.Text) <> "" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_mphone", SqlDbType.Decimal, 18)).Value = Convert.ToDecimal(Trim(txt_mobile2.Text))       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            End If

            cmd.Parameters.Add(New SqlParameter("@ctm_ctp", SqlDbType.NVarChar, 1)).Value = Trim(rblYesNo.SelectedValue)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            If rblYesNo.SelectedValue = "N" Then
                cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = ""
            Else
                If FileUpload1.FileName <> Nothing Then
                    cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = Trim(filename) 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
                Else
                    cmd.Parameters.Add(New SqlParameter("@ctm_ctpDoc", SqlDbType.NVarChar, 100)).Value = dt.Rows(0).Item("ctm_ctpDoc").ToString.Trim 'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
                End If
            End If

            cmd.Parameters.Add(New SqlParameter("@ctm_companyid", SqlDbType.NVarChar, 10)).Value = Trim(Session("g_Company"))            'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@ctm_userid", SqlDbType.NVarChar, 10)).Value = Trim(Session("g_User").ToString.Trim)
            cmd.Parameters.Add(New SqlParameter("@ctm_financialyearid", SqlDbType.NVarChar, 10)).Value = Session("g_FinYear").ToString          'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "client-master"                           'Add Parameter FormName and assign the name of form to it
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Delete"                                   'Add Parameter Mode and assign the DML Operation to Execute (Insert)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()                                                                 'Fill the Grid
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            getTableData()
            dt.Clear()
            gvClienteleDetail.SelectedIndex = -1
            ScriptManager1.SetFocus(btnADD)
            unLockControl(False)
            lnkFile.Visible = False
            DeleteClickVisible(Me, btnADD, btnSave, btnUpdate, btnDelete, btnCancel)
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try

    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("other-details.aspx")
    End Sub


    Protected Sub gvClienteleDetail_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvClienteleDetail.PageIndexChanging
        gvClienteleDetail.PageIndex = e.NewPageIndex
        getTableData()
    End Sub
End Class
