Imports System.Data
Imports System.Data.SqlClient
Partial Class GridSearch_Buyer
    Inherits System.Web.UI.UserControl
    Dim catid As String
    Dim name As String
    Dim dr As SqlDataReader
    Dim name1 As String
    Dim name2 As String
    Shared valuee1, valuee2, valuee3 As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        catid = Request.QueryString("catid")
        If Not IsPostBack Then
            valuee1 = ""
            valuee2 = ""
            valuee3 = ""
            Label1.Text = "You are here :- "
            loadcategory()
            System.Threading.Thread.Sleep(1000)
        End If
    End Sub

    Private Sub loadcategory()
        Try
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim Str As String
            Str = "select *from Category_Master where Category_ParentId='Root Category' order by category_name asc"
            If con.State = Data.ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(Str, con)
            Dim adp As New SqlDataAdapter
            Dim ds As New DataSet
            adp.SelectCommand = cmd
            adp.Fill(ds)
            GridView1.DataSource = ds
            GridView1.DataBind()
            Dim i As Integer
            Dim pcount As Integer
            pcount = GridView1.PageCount
            Dim j As Integer
            For j = 0 To GridView1.PageCount - 1
                GridView1.PageIndex = j
                GridView1.DataSource = ds
                GridView1.DataBind()
                For i = 0 To GridView1.Rows.Count - 1
                    Dim lnk As New LinkButton
                    lnk = GridView1.Rows(i).FindControl("LinkButton1")
                    If (lnk.CommandArgument = catid) Then
                        GridView1.SelectedIndex = i
                        Label1.Text += lnk.Text & "  >  "
                        valuee1 = Label1.Text
                        fillsubgrid()
                        GoTo lab
                    End If
                Next

            Next
            'For i = 0 To GridView1.Rows.Count - 1
            '    Dim lnk As New LinkButton
            '    lnk = GridView1.Rows(i).FindControl("LinkButton1")
            '    If (lnk.CommandArgument = catid) Then
            '        GridView1.SelectedIndex = i
            '        Label1.Text = lnk.Text & "  >  "
            '        valuee1 = Label1.Text
            '        fillsubgrid()
            '    End If
            'Next
lab:
            con.Close()
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub

    Public Sub fillsubgrid()
        Dim con1 As New SqlConnection
        con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        Dim Strr As String
        Dim lnk As New LinkButton
        lnk = GridView1.Rows(GridView1.SelectedIndex).FindControl("LinkButton1")
        Strr = "select *from Category_Master where Category_ParentId ='" & lnk.CommandArgument & "' order by category_name asc"
        If con1.State = Data.ConnectionState.Open Then
            con1.Close()
        End If
        con1.Open()
        Dim cmd1 As New SqlCommand(Strr, con1)
        'Dim dr1 As SqlDataReader
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet
        adp.SelectCommand = cmd1
        adp.Fill(ds)
        GridView2.DataSource = ds
        GridView2.DataBind()
        con1.Close()
    End Sub


    Public Sub filllastgrid()
        Dim con1 As New SqlConnection
        con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        Dim Strr As String
        Dim lnk As New LinkButton
        lnk = GridView2.Rows(GridView2.SelectedIndex).FindControl("LinkButton2")
        Strr = "select *from Category_Master where Category_ParentId ='" & lnk.CommandArgument & "' order by category_Name asc"
        If con1.State = Data.ConnectionState.Open Then
            con1.Close()
        End If
        con1.Open()
        Dim cmd1 As New SqlCommand(Strr, con1)
        'Dim dr1 As SqlDataReader
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet
        adp.SelectCommand = cmd1
        adp.Fill(ds)
        GridView3.DataSource = ds
        GridView3.DataBind()
        con1.Close()
    End Sub
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        loadcategoryyy()
        Dim dt As New DataTable
        GridView3.DataSource = dt
        GridView3.DataBind()
        GridView1.SelectedIndex = 0
        Dim lnk As New LinkButton
        lnk = GridView1.Rows(GridView1.SelectedIndex).FindControl("LinkButton1")
        Label1.Text = lnk.Text
        fillsubgrid()
    End Sub

    Private Sub loadcategoryyy()
        Try
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim Str As String
            Str = "select * from Category_Master where Category_ParentId='Root Category' order by category_name asc"
            If con.State = Data.ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(Str, con)
            Dim adp As New SqlDataAdapter
            Dim ds As New DataSet
            adp.SelectCommand = cmd
            adp.Fill(ds)
            GridView1.DataSource = ds
            GridView1.DataBind()
            GridView1.SelectedIndex = 0
            con.Close()
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub
    Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView2.PageIndexChanging
        GridView2.PageIndex = e.NewPageIndex
        fillsubgrid()
    End Sub
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim lnk As New LinkButton
            lnk = sender
            Label1.Text = valuee3 & lnk.Text & "  >  "
            valuee1 = Label1.Text

            Dim i As String
            Dim i1 As Integer
            i = lnk.CommandArgument
            For i1 = 0 To GridView1.Rows.Count - 1
                Dim lnkk As New LinkButton
                lnkk = GridView1.Rows(i1).FindControl("LinkButton1")
                If (lnkk.CommandArgument = i) Then
                    GridView1.SelectedIndex = i1
                End If
            Next
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim Strr As String
            Strr = "select *from Category_Master where Category_ParentId ='" & i & "' order by category_name asc"
            If con.State = Data.ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(Strr, con)
            dr = cmd.ExecuteReader()
            If dr.Read Then
                Dim adp As New SqlDataAdapter
                Dim ds As New DataSet
                adp.SelectCommand = cmd
                dr.Close()
                adp.Fill(ds)
                GridView2.DataSource = ds
                GridView2.DataBind()
                con.Close()
                Dim dt As New DataTable
                GridView3.DataSource = dt
                GridView3.DataBind()
            Else
                Session("HLabel") = Label1.Text
                If Session("ID") = "Buyer" Then
                    Response.Redirect("BuyerProdSearch.aspx?Product_Id=" & EncodeURL(i))
                Else
                    Response.Redirect("SearchProduct.aspx?Product_Id=" & EncodeURL(i))
                End If
                ' Response.Redirect("SearchProduct.aspx?Product_Id=" & i)
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim lnk As New LinkButton
            lnk = sender
            Label1.Text = valuee1 & lnk.Text & "  >  "
            valuee2 = Label1.Text



            Dim i As String
            Dim i1 As Integer
            i = lnk.CommandArgument
            For i1 = 0 To GridView2.Rows.Count - 1
                Dim lnkk As New LinkButton
                lnkk = GridView2.Rows(i1).FindControl("LinkButton2")
                If (lnkk.CommandArgument = i) Then
                    GridView2.SelectedIndex = i1
                End If
            Next
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim Strr As String
            Strr = "select *from Category_Master where Category_ParentId ='" & i & "' order by category_name asc"
            If con.State = Data.ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(Strr, con)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader
            If dr.Read Then
                Dim adp As New SqlDataAdapter
                Dim ds As New DataSet
                adp.SelectCommand = cmd
                dr.Close()
                adp.Fill(ds)
                GridView3.DataSource = ds
                GridView3.DataBind()
                con.Close()
            Else
                Session("HLabel") = Label1.Text
                If Session("ID") = "Buyer" Then
                    Response.Redirect("BuyerProdSearch.aspx?Product_Id=" & EncodeURL(i))
                Else
                    Response.Redirect("SearchProduct.aspx?Product_Id=" & EncodeURL(i))
                End If
                'Response.Redirect("SearchProduct.aspx?Product_Id=" & i)
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub

    Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim lnk As New LinkButton
            lnk = sender
            Label1.Text = valuee2 & lnk.Text & "  >  "
            valuee3 = Label1.Text



            Dim i As String
            Dim i1 As Integer
            i = lnk.CommandArgument
            For i1 = 0 To GridView3.Rows.Count - 1
                Dim lnkk As New LinkButton
                lnkk = GridView3.Rows(i1).FindControl("LinkButton3")
                If (lnkk.CommandArgument = i) Then
                    GridView3.SelectedIndex = i1
                End If
            Next
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            Dim Strr As String
            Strr = "select *from Category_Master where Category_ParentId ='" & i & "' order by category_name asc"
            If con.State = Data.ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(Strr, con)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader
            If dr.Read Then
                Dim adp As New SqlDataAdapter
                Dim ds As New DataSet
                adp.SelectCommand = cmd
                dr.Close()
                adp.Fill(ds)
                GridView1.DataSource = ds
                GridView1.DataBind()
                con.Close()
                Dim dt As New DataTable
                GridView2.DataSource = dt
                GridView2.DataBind()
                GridView3.DataSource = dt
                GridView3.DataBind()
            Else
                Session("HLabel") = Label1.Text
                If Session("ID") = "Buyer" Then
                    Response.Redirect("BuyerProdSearch.aspx?Product_Id=" & EncodeURL(i))
                Else
                    Response.Redirect("SearchProduct.aspx?Product_Id=" & EncodeURL(i))
                End If
                'Response.Redirect("SearchProduct.aspx?Product_Id=" & i)
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub

    Protected Sub GridView3_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        GridView3.PageIndex = e.NewPageIndex
        filllastgrid()
    End Sub
End Class
