Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class categorysearch
    Inherits System.Web.UI.Page
    Shared Category(10) As String
    Shared i As Integer
    Shared Inquery_kid As String
    Shared Inquery_code As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            txtProductName.Focus()
            i = 0

        End If
        pnlPostQ.Visible = False
    End Sub
    Protected Sub iSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles iSearch.Click

        fillgrid()

        If gvSearch.Rows.Count = 0 Then
            pnlPostQ.Visible = True
            lblNoOf.Visible = False
            clearfields()
        End If
    End Sub
    Public Sub fillgrid()
        Dim dt As DataTable
        dt = ReturnDataTable("SELECT Product_Kid, Product_Name, Product_ModelNo, Product_CategoryId FROM Product_Master WHERE Product_Approved = 'Y' and Product_Name LIKE '" & txtProductName.Text.ToString.Trim & "%'")
        gvSearch.Columns(0).Visible = True
        gvSearch.DataSource = dt
        gvSearch.DataBind()
        gvSearch.Columns(0).Visible = False

        lblNoOf.Text = dt.Rows.Count & "  Records found."

        Dim j As Integer
        For j = 0 To gvSearch.Rows.Count - 1
            Dim catid As String
            catid = ReturnValue("Select Product_CategoryID from Product_Master where Product_Kid= '" & RemoveLiterals(gvSearch.Rows(j).Cells(0).Text.ToString.Trim) & "'and Product_IsDeleted='0'")
            i = 0
            FillCategoryName(catid, CType(gvSearch.Rows(j).Cells(1).FindControl("lblCategory"), Label))
        Next

    End Sub
    Public Sub FillCategoryName(ByVal Catid As String, ByVal lbl As Label)
        Dim dt As DataTable
        dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0'")
        Dim ParentId As String
        ParentId = dt.Rows(0).Item(3).ToString()
        If ParentId = "Root Category" Then
            'Label1.Text += dt.Rows(0).Item(2).ToString()
            'Dim CategoryName(10) As String
            Dim j As Integer
            Category(i) = dt.Rows(0).Item(2).ToString() & "  -  "
            'Array.Reverse(Category)
            For j = 0 To i
                lbl.Text += Category(i)
                i = i - 1
            Next
            Exit Sub
        Else
            Category(i) = dt.Rows(0).Item(2).ToString() & "  -  "
            i = i + 1
            'Label1.Text += dt.Rows(0).Item(2).ToString() & "  >  "
            FillCategoryName(dt.Rows(0).Item(3).ToString(), lbl)
        End If
        'GridView1.DataSource = dt
        'GridView1.DataBind()
    End Sub

    Protected Sub gvSearch_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvSearch.PageIndexChanging
        gvSearch.PageIndex = e.NewPageIndex
        fillgrid()
    End Sub

    Protected Sub btnDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btn As Button
        Dim l As New Label
        btn = sender
        Dim catid As String
        catid = ReturnValue("Select Product_CategoryID from Product_Master where Product_Kid= '" & RemoveLiterals(btn.CommandArgument.ToString.Trim) & "'and Product_IsDeleted='0'")
        i = 0
        FillCategoryName(catid, l)
        Session("HLabel") = l.Text
        Session("SPBack") = "Yes"
        Response.Redirect("searchProductDetails.aspx?ProductCode=" & RemoveLiterals(btn.CommandArgument.ToString.Trim))
    End Sub

    Public Sub clearfields()
        txtQEmail.Text = ""
        txtQMobileNo.Text = ""
        txtQProductName.Text = ""
    End Sub

    Protected Sub iCLose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles iCLose.Click
        pnlPostQ.Visible = False
    End Sub

    Protected Sub btnSubmitQ_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitQ.Click
        Try

            GenerateInqueryCode()
            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()
            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 = "Inquery_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@Inquery_Kid", SqlDbType.NVarChar, 10)).Value = RemoveLiterals(Inquery_kid.ToString.Trim)
            cmd.Parameters.Add(New SqlParameter("@Inquery_Code", SqlDbType.NVarChar, 25)).Value = Inquery_code       'Add Parameter Banke_Code and assign the value of txtBankCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Inquery_ProductName", SqlDbType.NVarChar, 100)).Value = Trim(txtQProductName.Text)       'Add Parameter Bank_Name and assign the value of txtBankName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Inquery_EmailId", SqlDbType.NVarChar, 50)).Value = Trim(txtQEmail.Text)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            If txtQMobileNo.Text <> "" Then
                cmd.Parameters.Add(New SqlParameter("@Inquery_MobileNo", SqlDbType.NVarChar, 50)).Value = Trim(txtQMobileNo.Text)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            End If
            cmd.Parameters.Add(New SqlParameter("@Inquery_Date", SqlDbType.DateTime)).Value = DateTime.Now
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 50)).Value = "Inquery"                           'Add Parameter FormName and assign the name of form to it
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 10)).Value = "Insert"                                   'Add Parameter Mode and assign the DML Operation to Execute (Insert)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            pnlPostQ.Visible = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
    Protected Sub GenerateInqueryCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            Inquery_kid = NewPrimaryKey(Me)
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "INQ" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from Inquery where Inquery_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    Inquery_Code = code
                    Exit Sub
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
End Class
