Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Partial Class Product_Review
    Inherits System.Web.UI.Page
    Shared Search As Boolean
    Shared g_User As String
    Shared img As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("SupplierID") Is Nothing And Session("AdminID") = Nothing Then
            Response.Redirect("listproduct.aspx?sess=expire")
            Exit Sub
        End If
        Try
            If (Not IsPostBack) Then
                rwCode.Visible = False
                'If IsPostBack is false then
                If Session("ProductID") = Nothing Then
                    Response.Redirect("List_Product.aspx")
                End If

                btnDelete.Attributes.Add("onclick", "return ConfrmMsg();")
                BtnUpdate.Attributes.Add("onclick", "return UpdateMsg();")
                BtnSave.Attributes.Add("onclick", "return SaveConfirm();")

                ClearFields()               'Clear the TextFields
                'g_User = Session("g_User").ToString
                g_User = Session("SupplierID").ToString.Trim
                g_Company = Session("g_Company").ToString
                Call GenerateBankCode()
                getGridData()
                ddl_ProReviewOtherProduct.Visible = False
                lblExisting.Visible = False
                'UnLockTextBox(False)
                getExistingData()
                lblClientCat.Text = createheaderlabel(Session("ProductId"))
                Call CheckRights(Me, Session("SupplierID").ToString.Trim, "Bank Master")
            End If
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Public Sub getGridData()

        Fill_Grid("SELECT     ProductReview.*    FROM ProductReview WHERE     ProductReview_productid = '" & Session("ProductId") & "' AND ProductReview_companyid = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'", gvProductReview)
        getTableData()

    End Sub

    Public Sub getTableData()
        Try
            Dim dt As DataTable = ReturnDataTable("SELECT     ProductReview.*    FROM ProductReview WHERE     ProductReview_productid = '" & Session("ProductId") & "' AND ProductReview_companyid = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'")
            If dt.Rows.Count > 0 Then
                Dim i As Integer
                If Session("AdminID") <> Nothing Or Session("AdminID") = Nothing Then
                    gvProductReview.Columns(0).Visible = True
                Else
                    gvProductReview.Columns(0).Visible = False
                End If

                For i = 0 To gvProductReview.Rows.Count - 1
                    Dim lblDoc As HyperLink = CType(gvProductReview.Rows(i).Cells(3).FindControl("lblFileName"), HyperLink)
                    If dt.Rows(i).Item("ProductReview_FileName").ToString <> "" Then
                        lblDoc.Text = dt.Rows(i).Item("ProductReview_FileName").ToString
                        lblDoc.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(i).Item("ProductReview_FileName").ToString
                    Else
                        lblDoc.Text = "-"
                    End If
                Next
            Else
                Call GenerateBankCode()
                BtnSave.Visible = True
                btnAddMore.Visible = True
            End If
            If Session("AdminID") <> Nothing Or Session("AdminID") = Nothing Then
                ProRevwFileUpload.Visible = True
                tr1.Visible = True
                If dt.Rows.Count > 0 Then
                    UnLockTextBox(True)
                    BtnUpdate.Visible = True
                    BtnSave.Enabled = False
                    btnAddMore.Visible = False
                Else
                    UnLockTextBox(True)
                    BtnUpdate.Visible = False
                    BtnSave.Visible = True
                    btnAddMore.Visible = True
                End If
            Else
                BtnUpdate.Visible = False
                'UnLockTextBox(False)
            End If
            dt.Clear()

            BtnUpdate.Visible = False
            btnDelete.Visible = False

        Catch
        End Try

    End Sub

    Protected Function createheaderlabel(ByVal Prodid As String) As String
        Try
            Dim lblhead As String
            'If ddlProductList.SelectedIndex <> 0 Then
            Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
            Dim str As String = "SELECT Category_Master.Category_Kid, Category_Master.Category_Name FROM         Product_Information INNER JOIN                     Category_Master ON Product_Information.Product_CategoryId = Category_Master.Category_Kid WHERE     Product_Information.Product_kid = '" & Prodid & "' AND (Category_Master.Category_IsDeleted = N'0')"
            Dim ds As New DataSet
            Dim sda As New SqlDataAdapter(str, con)
            sda.Fill(ds)

            Dim parent As String = ds.Tables(0).Rows(0)(1).ToString()

            lblhead = parent & ">"
            While parent <> "Root Category"
                parent = ReturnValue("SELECT     Category_Master.Category_Name AS Expr2 FROM         Category_Master INNER JOIN                    Category_Master AS Category_Master_1 ON Category_Master.Category_Kid = Category_Master_1.Category_ParentId WHERE     (Category_Master.Category_IsDeleted = N'0') AND (Category_Master.Category_IsDeleted = N'0') AND Category_Master_1.Category_Name = '" & parent & "'")
                If parent = "" Then
                    Exit While
                End If
                lblhead = parent & ">" & lblhead
            End While
            'Else
            '    lblhead = ""
            'End If
            Return lblhead
            ds.Clear()
        Catch
        End Try
    End Function

    Private Sub UnLockControls(ByVal bool As Boolean)
        txtProductDetails.Enabled = bool
        ProRevwFileUpload.Enabled = bool

    End Sub


    Private Sub getExistingData()
        Try
            Dim dt As DataTable = getTable("Select ProductReview_Kid,ProductReview_Code,ProductReview_FileName,ProductReview_ProductDetails from ProductReview where ProductReview_ProductId='" & Session("ProductID").ToString.Trim & "' and ProductReview_IsDeleted='0' and ProductReview_CompanyID='" & Session("g_Company").ToString.Trim & "'", "PR")
            If dt.Rows.Count > 0 Then


                'txtProductReviewKid.Text = dt.Rows(0).Item("ProductReview_Kid").ToString
                'txtProductReviewCode.Text = dt.Rows(0).Item("ProductReview_Code").ToString
                'txtProductDetails.Text = dt.Rows(0).Item("ProductReview_ProductDetails").ToString
                'Try
                '    If IsDBNull(dt.Rows(0).Item("ProductReview_FileName").ToString) Then
                '        tr1.Visible = True
                '        tr2.Visible = False
                '    Else
                '        tr1.Visible = False
                '        tr2.Visible = True
                '        lblResult.Visible = True
                '        lblResult.Text = dt.Rows(0).Item("ProductReview_FileName").ToString
                '        lblResult.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(0).Item("ProductReview_FileName").ToString
                '        lblResult.Enabled = True
                '    End If
                'Catch
                'End Try
                BtnSave.Enabled = False
                Label13.Visible = False
                lblExisting.Visible = True
                ddl_ProReviewOtherProduct.Visible = True
                ImageButton1.Visible = True
                Try
                    FillExistingDropDown("SELECT ProductReview_ProductId FROM ProductReview WHERE   ProductReview_UserId = '" & Session.Item("SupplierID").ToString.Trim & "' and ProductReview_ProductId <> '" & RemoveLiterals(Session("ProductID").ToString()) & "' AND ProductReview_companyid = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'", ddl_ProReviewOtherProduct)
                Catch
                End Try
                'ddl_ProReviewOtherProduct.Visible = False
                'lblExisting.Visible = False
                'UnLockTextBox(False)
                'ImageButton1.Visible = False
                'Label13.Visible = False
            ElseIf dt.Rows.Count = 0 Then
                Label13.Visible = False
                lblExisting.Visible = True
                ddl_ProReviewOtherProduct.Visible = True
                ImageButton1.Visible = True
                Try
                    FillExistingDropDown("SELECT ProductReview_ProductId FROM ProductReview WHERE ProductReview_UserId = '" & Session.Item("SupplierID").ToString.Trim & "' and ProductReview_ProductId <> '" & RemoveLiterals(Session("ProductID").ToString()) & "' AND ProductReview_companyid = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'", ddl_ProReviewOtherProduct)
                Catch
                End Try
                'Call GenerateBankCode()
                BtnSave.Visible = True
            End If

            'If ddl_ProReviewOtherProduct.Items.Count = 0 Then
            '    UnLockTextBox(False)
            'End If
            If Session("AdminID") <> Nothing Or Session("AdminID") = Nothing Then
                ProRevwFileUpload.Visible = True
                tr1.Visible = True
                If dt.Rows.Count > 0 Then
                    UnLockTextBox(True)
                    BtnUpdate.Visible = True
                    btnDelete.Visible = True
                    btnAddMore.Visible = True
                    BtnSave.Enabled = False
                Else
                    UnLockTextBox(True)
                    BtnUpdate.Visible = True
                    BtnUpdate.Enabled = False
                    BtnUpdate.Visible = True
                    BtnUpdate.Enabled = False
                    BtnSave.Visible = True
                    btnAddMore.Visible = True
                End If
            Else
                BtnUpdate.Visible = False
                btnDelete.Visible = False
                'UnLockTextBox(False)
            End If

            If dt.Rows.Count > 0 Then
                BtnUpdate.Visible = False
                btnDelete.Visible = False

            End If

            dt.Clear()
            'BtnSave.Visible = False
            BtnSave.Enabled = True
            btnAddMore.Visible = False
        Catch
        End Try
    End Sub

    Public Sub FillExistingDropDown(ByVal strQuery As String, ByVal ddl As DropDownList)
        Try
            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
            Try
                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_Combo("Select Product_Kid,Product_Name from Product_Master where Product_SupplierID='" & Session.Item("SupplierID").ToString.Trim & "' and Product_UserID = '" & Session.Item("SupplierID").ToString().Trim & "' and Product_Kid In ( " & Kid & ")", ddl)
            Catch
            End Try
            If ddl.Items.Count > 0 Then
                ddl.Items.Insert(ddl.Items.Count, "Add New")
                lblExisting.Visible = True
                UnLockControls(False)
                ImageButton1.Visible = True
            Else
                ImageButton1.Visible = False
                lblExisting.Visible = False
                ddl.Visible = False
                UnLockControls(True)
            End If
        Catch
        End Try
    End Sub



    Protected Sub _Default_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Try
            If Not IsValidConnectionString() Then CreateMessageAlert(Me, "Check the Connection String !", "StrKeyVal") 'If not a valid ConnectionString then Display the message 
        Catch
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="ClearFields">
    '   <Desc> For Clearing the contents of TextFields </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 30/07/07</DateOfCreation>
    '   <Developer> Neeta </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            'txtProductReviewCode.Text = ""                           'Assign Null to the TextField
            txtProductDetails.Text = ""                           'Assign Null to the TextField
            lblResult.Text = ""
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="UnLockTextBox">
    '   <Desc> For Locking & UnLocking the TextFields </Desc>
    '   <Input> 
    '   <Param Name="Action">
    '   True or False
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 30/07/07 </DateOfCreation>
    '   <Developer> Neeta </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            'txtBankCode.Enabled = Action                    'Enable / Disable the TextField
            txtProductDetails.Enabled = Action                    'Enable / Disable the TextField
            ProRevwFileUpload.Enabled = Action
            'lblResult.enabled = action
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="FillGrid">
    '   <Desc> To Fill the Grid with All Records </Desc>
    '   <Input> 
    '  <Param Name="PTN">
    '   PartType_Name
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 30/07/07 </DateOfCreation>
    '   <Developer> Neeta </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub FillGrid(ByVal PTN As String)
        Try

            Dim qry As String                                   'Define a String Variable for Query
            Dim con As New SqlConnection                        'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                          'Open the Connection
            If Trim(PTN) = "" Then
                qry = "select distinct ProductReview_Kid,ProductReview_Code, Product_Master.Product_Name as [Product Name], ProductReview_ProductDetails from ProductReview,Product_Master where Product_Master.Product_Kid = ProductReview.ProductReview_ProductId and ProductReview_IsDeleted='0' and ProductReview_CompanyId='" & Session("g_Company").ToString & "'"
            Else
                qry = "select distinct ProductReview_Kid,ProductReview_Code, Product_Master.Product_Name as [Product Name], ProductReview_ProductDetails from ProductReview,Product_Master where Product_Master.Product_Kid = ProductReview.ProductReview_ProductId and ProductReview_IsDeleted='0' and ProductReview_CompanyId='" & Session("g_Company").ToString & "' and ProductReview_ProductDetails like '" & Trim(PTN) & "%'"
            End If
            Dim cmd As New SqlCommand(qry, con)                 'Define command variable to execute the query
            Dim dr As SqlDataReader                             'Define DataReader variable for storing the output of the query
            dr = cmd.ExecuteReader                              'Assign the output of query to DataReader variable
        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
        'Save Item in Product Review
        Try
            If Trim(txtProductDetails.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Product Details Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            Dim strImageName As String = ""
            Dim file_name As String = ""
            If (ProRevwFileUpload.HasFile) Then
                If Path.GetExtension(ProRevwFileUpload.FileName).ToLower = ".pdf" Then
                    strImageName = Final_FileUpload(ProRevwFileUpload, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))

                Else
                    lblResult.Visible = True
                    lblResult.Text = "Please Select .pdf File Only!"
                    ProRevwFileUpload.Focus()
                    Exit Sub
                End If
            ElseIf lblResult.Text.Length > 0 And lblResult.Text <> "Please Select .pdf File Only!" Then
            Else
                lblResult.Visible = True
                lblResult.Text = "Please Select .pdf File Only!"
                ProRevwFileUpload.Focus()
                Exit Sub
            End If

            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

            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 = "ProductReview_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Kid", SqlDbType.NVarChar, 20)).Value = NewPrimaryKey(Me)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Code", SqlDbType.NVarChar, 20)).Value = GenerateBankCode().ToString()        'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductDetails", SqlDbType.NVarChar, 25)).Value = Trim(txtProductDetails.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductId", SqlDbType.NVarChar, 20)).Value = Session("ProductID")
            If (ProRevwFileUpload.HasFile) Then
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = strImageName
            Else
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = lblResult.Text
            End If
            cmd.Parameters.Add(New SqlParameter("@ProductReview_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear").ToString
            cmd.Parameters.Add(New SqlParameter("@ProductReview_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_Company").ToString)            'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("SupplierID").ToString.Trim)             'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Product Review"                           '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()                                                           'Execute the Query
            con.Close()
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            UnLockControls(False)
            getGridData()

            'Response.Redirect("ProductDeliverydetails.aspx")
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub



    Protected Sub txtProductDetails_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDetails.TextChanged
        Try
            If Search = True Then                               'If Search Mode is true then
                Call FillGrid(Trim(txtProductDetails.Text))  'Fill the Grid filtering on txtStockLocationName 
            End If
            If BtnSave.Enabled Then BtnSave.Focus()
        Catch
        End Try
    End Sub
    Public Function GenerateBankCode() 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 = "PRV" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from ProductReview where ProductReview_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                    Return code
                    Exit Function
                Else
                    Return code
                    Exit Function
                End If
            End While
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Function


    Sub Node_Populate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs)
        Try
            If e.Node.ChildNodes.Count = 0 Then
                Select Case (e.Node.Depth)
                    Case 0

                        FillModule(e.Node, "2")

                        Exit Sub
                    Case 1

                        FillMenu(e.Node)

                        Exit Sub

                    Case 2

                        FillSubMenu(e.Node)

                        Exit Sub

                End Select

            End If
        Catch
        End Try
    End Sub

    Protected Sub BtnSkip_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Response.Redirect("supplier-keycontacts.aspx")
        Catch
        End Try
    End Sub

    Protected Sub ddl_ProReviewOtherProduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_ProReviewOtherProduct.SelectedIndexChanged
        Try
            If ddl_ProReviewOtherProduct.SelectedIndex > 0 Then
                If ddl_ProReviewOtherProduct.SelectedItem.Text = "Add New" Then
                    UnLockControls(True)
                    ClearFields()
                Else
                    UnLockControls(True)
                    Dim Kid As String = ddl_ProReviewOtherProduct.SelectedItem.Value

                    Fill_Grid("SELECT ProductReview.*  FROM ProductReview WHERE ProductReview_ProductId = '" & Kid.ToString.Trim & "' AND ProductReview_CompanyId = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'", gvProductReviewExisting)
                    getTableDataExisintg()


                    'Dim dt As DataTable = getTable("SELECT ProductReview.*  FROM ProductReview WHERE ProductReview_ProductId = '" & Kid.ToString.Trim & "' AND ProductReview_CompanyId = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'", "PR")
                    'If dt.Rows.Count > 0 Then

                    '    txtProductDetails.Text = dt.Rows(0).Item("ProductReview_ProductDetails").ToString
                    '    tr1.Visible = True
                    '    tr2.Visible = True
                    '    lblResult.Visible = True
                    '    lblResult.Text = dt.Rows(0).Item("ProductReview_FileName").ToString
                    '    lblResult.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(0).Item("ProductReview_FileName").ToString

                    '    UnLockControls(True)


                    'End If

                End If
            End If
        Catch
        End Try
    End Sub

    Public Sub getTableDataExisintg()
        Try
            Dim Kid As String = ddl_ProReviewOtherProduct.SelectedItem.Value

            Dim dt As DataTable = ReturnDataTable("SELECT ProductReview.*  FROM ProductReview WHERE ProductReview_ProductId = '" & Kid.ToString.Trim & "' AND ProductReview_CompanyId = '" & Session("g_Company").ToString & "' AND ProductReview_IsDeleted = N'0'")
            If dt.Rows.Count > 0 Then
                Label13.Visible = True
                Dim i As Integer
                'If Session("AdminID") <> Nothing Then
                '    gvProductReview.Columns(0).Visible = True
                'Else
                '    gvProductReview.Columns(0).Visible = False
                'End If

                For i = 0 To gvProductReviewExisting.Rows.Count - 1
                    Dim lblDoc As HyperLink = CType(gvProductReviewExisting.Rows(i).Cells(3).FindControl("lblFileNameExisting"), HyperLink)
                    If dt.Rows(i).Item("ProductReview_FileName").ToString <> "" Then
                        lblDoc.Text = dt.Rows(i).Item("ProductReview_FileName").ToString
                        lblDoc.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(i).Item("ProductReview_FileName").ToString
                    Else
                        lblDoc.Text = "-"
                    End If
                Next
            Else
                'Call GenerateBankCode()
                'BtnSave.Visible = True
                'btnAddMore.Visible = True
            End If

            dt.Clear()
        Catch
        End Try

    End Sub

    Protected Sub BtnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
        Try

            'Dim kid As String = gvProductReviewExisting.SelectedDataKey.Value.ToString

            Dim dt As DataTable = ReturnDataTable("SELECT ProductReview.* FROM ProductReview WHERE ProductReview_Kid = '" & gvProductReview.SelectedDataKey.Value.ToString & "' AND ProductReview_IsDeleted = '0'")

            If Trim(txtProductDetails.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Product Details Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            Dim strImageName As String = ""
            Dim file_name As String = ""
            If (ProRevwFileUpload.HasFile) Then
                If Path.GetExtension(ProRevwFileUpload.FileName).ToLower = ".pdf" Then
                    strImageName = Final_FileUpload(ProRevwFileUpload, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))
                Else
                    lblResult.Visible = True
                    lblResult.Text = "Please Select .pdf File Only!"
                    ProRevwFileUpload.Focus()
                    Exit Sub
                End If
            ElseIf dt.Rows(0).Item("ProductReview_FileName").ToString.Length > 0 Then
            Else
                lblResult.Visible = True
                lblResult.Text = "Please Select .pdf File Only!"
                ProRevwFileUpload.Focus()
                Exit Sub
            End If

            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

            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 = "ProductReview_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Kid", SqlDbType.NVarChar, 20)).Value = dt.Rows(0).Item("ProductReview_Kid").ToString       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Code", SqlDbType.NVarChar, 20)).Value = dt.Rows(0).Item("ProductReview_Code").ToString        'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductDetails", SqlDbType.NVarChar, 25)).Value = Trim(txtProductDetails.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductId", SqlDbType.NVarChar, 20)).Value = Session("ProductID")
            If (ProRevwFileUpload.HasFile) Then
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = strImageName
            Else
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = lblResult.Text
            End If
            cmd.Parameters.Add(New SqlParameter("@ProductReview_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear").ToString
            cmd.Parameters.Add(New SqlParameter("@ProductReview_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_Company").ToString)            'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("SupplierID").ToString.Trim)             'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Product Review"                           '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()
            getGridData()
            con.Close()
            CreateMessageAlert(Me, "Record Updated Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            btnAddMore.Visible = True
            dt.Clear()
            'Response.Redirect("ProductDeliverydetails.aspx")
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub btnAddMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMore.Click
        Try
            If Trim(txtProductDetails.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Product Details Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            Dim strImageName As String = ""
            Dim file_name As String = ""
            If (ProRevwFileUpload.HasFile) Then
                If Path.GetExtension(ProRevwFileUpload.FileName).ToLower = ".pdf" Then
                    strImageName = Final_FileUpload(ProRevwFileUpload, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))
                Else
                    lblResult.Visible = True
                    lblResult.Text = "Please Select .pdf File Only!"
                    ProRevwFileUpload.Focus()
                    Exit Sub
                End If
            ElseIf lblResult.Text.Length > 0 And lblResult.Text <> "Please Select .pdf File Only!" Then
            Else
                lblResult.Visible = True
                lblResult.Text = "Please Select .pdf File Only!"
                ProRevwFileUpload.Focus()
                Exit Sub
            End If

            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

            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 = "ProductReview_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Kid", SqlDbType.NVarChar, 20)).Value = NewPrimaryKey(Me)       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Code", SqlDbType.NVarChar, 20)).Value = GenerateBankCode().ToString        'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductDetails", SqlDbType.NVarChar, 25)).Value = Trim(txtProductDetails.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductId", SqlDbType.NVarChar, 20)).Value = Session("ProductID")
            If (ProRevwFileUpload.HasFile) Then
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = strImageName
            Else
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = lblResult.Text
            End If
            cmd.Parameters.Add(New SqlParameter("@ProductReview_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear").ToString
            cmd.Parameters.Add(New SqlParameter("@ProductReview_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_Company").ToString)            'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("SupplierID").ToString.Trim)             'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Product Review"                           '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()                                                           'Execute the Query
            con.Close()
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            getGridData()
            Call GenerateBankCode()
            UnLockTextBox(True)
            BtnSave.Enabled = False
            btnAddMore.Visible = True
            'Response.Redirect("ProductDeliverydetails.aspx")
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub gvProductReview_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim kid As String = gvProductReview.SelectedDataKey.Value.ToString

            Dim dt As DataTable = getTable("Select * from ProductReview where ProductReview_Kid='" & RemoveLiterals(kid.ToString) & "'", "PR")
            If dt.Rows.Count > 0 Then
                txtProductReviewKid.Text = dt.Rows(0).Item("ProductReview_Kid").ToString
                txtProductReviewCode.Text = dt.Rows(0).Item("ProductReview_Code").ToString
                txtProductDetails.Text = dt.Rows(0).Item("ProductReview_ProductDetails").ToString
                Try
                    If IsDBNull(dt.Rows(0).Item("ProductReview_FileName").ToString) Then
                        tr1.Visible = True
                        tr2.Visible = False
                    Else
                        tr1.Visible = True
                        tr2.Visible = True
                        lblResult.Visible = True
                        lblResult.Text = dt.Rows(0).Item("ProductReview_FileName").ToString
                        lblResult.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(0).Item("ProductReview_FileName").ToString
                        lblResult.Enabled = True
                    End If
                Catch
                End Try
                BtnSave.Visible = False
                BtnUpdate.Visible = True
                btnDelete.Visible = True
                'ddl_ProReviewOtherProduct.Visible = False
                'lblExisting.Visible = False
                UnLockTextBox(True)
                'ImageButton1.Visible = False
                dt.Clear()
            End If
        Catch
        End Try
    End Sub

    Protected Sub gvProductReviewExisting_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim kid As String = gvProductReviewExisting.SelectedDataKey.Value.ToString

            Dim dt As DataTable = getTable("Select * from ProductReview where ProductReview_Kid='" & RemoveLiterals(kid.ToString) & "'", "PR")
            If dt.Rows.Count > 0 Then
                txtProductReviewKid.Text = dt.Rows(0).Item("ProductReview_Kid").ToString
                txtProductReviewCode.Text = dt.Rows(0).Item("ProductReview_Code").ToString
                txtProductDetails.Text = dt.Rows(0).Item("ProductReview_ProductDetails").ToString
                Try
                    If IsDBNull(dt.Rows(0).Item("ProductReview_FileName").ToString) Then
                        tr1.Visible = True
                        tr2.Visible = False
                    Else
                        tr1.Visible = True
                        tr2.Visible = True
                        lblResult.Visible = True
                        lblResult.Text = dt.Rows(0).Item("ProductReview_FileName").ToString
                        lblResult.NavigateUrl = "~\Upload Image\Product Image\UploadImages\" & dt.Rows(0).Item("ProductReview_FileName").ToString
                        lblResult.Enabled = True
                    End If
                Catch
                End Try
                'BtnSave.Visible = False
                'ddl_ProReviewOtherProduct.Visible = False
                'lblExisting.Visible = False
                UnLockTextBox(True)

                'ImageButton1.Visible = False
                dt.Clear()
            End If
        Catch
        End Try
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

            Dim dt As DataTable = ReturnDataTable("SELECT ProductReview.* FROM ProductReview WHERE ProductReview_Kid = '" & gvProductReview.SelectedDataKey.Value.ToString & "' AND ProductReview_IsDeleted = '0'")

            If Trim(txtProductDetails.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Product Details Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            Dim strImageName As String = ""
            Dim file_name As String = ""
            If (ProRevwFileUpload.HasFile) Then
                If Path.GetExtension(ProRevwFileUpload.FileName).ToLower = ".pdf" Then
                    strImageName = Final_FileUpload(ProRevwFileUpload, img, Server.MapPath("~\Upload Image\Product Image\UploadImages\"))
                Else
                    lblResult.Visible = True
                    lblResult.Text = "Please Select .pdf File Only!"
                    ProRevwFileUpload.Focus()
                    Exit Sub
                End If
            ElseIf dt.Rows(0).Item("ProductReview_FileName").ToString.Length > 0 Then
            Else
                lblResult.Visible = True
                lblResult.Text = "Please Select .pdf File Only!"
                ProRevwFileUpload.Focus()
                Exit Sub
            End If

            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

            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 = "ProductReview_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Kid", SqlDbType.NVarChar, 20)).Value = dt.Rows(0).Item("ProductReview_Kid").ToString       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_Code", SqlDbType.NVarChar, 20)).Value = dt.Rows(0).Item("ProductReview_Code").ToString        'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductDetails", SqlDbType.NVarChar, 25)).Value = Trim(txtProductDetails.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_ProductId", SqlDbType.NVarChar, 20)).Value = Session("ProductID")
            If (ProRevwFileUpload.HasFile) Then
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = strImageName
            Else
                cmd.Parameters.Add(New SqlParameter("@ProductReview_FileName", SqlDbType.NVarChar, 20)).Value = lblResult.Text
            End If
            cmd.Parameters.Add(New SqlParameter("@ProductReview_FinancialYearId", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear").ToString
            cmd.Parameters.Add(New SqlParameter("@ProductReview_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(Session("g_Company").ToString)            'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@ProductReview_UserId", SqlDbType.NVarChar, 20)).Value = Trim(Session("SupplierID").ToString.Trim)             'Add Parameter UserCode and assign the value of Session("Session("SupplierID").ToString.Trim").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Product Review"                           '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()
            getGridData()
            con.Close()
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()
            btnAddMore.Visible = True
            dt.Clear()
            'Response.Redirect("ProductDeliverydetails.aspx")
        Catch ex As Exception
            'CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
End Class
