Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class category_pagelink
    Inherits System.Web.UI.Page
    Shared Search As Boolean
    Shared g_User As String
    Shared g_Company As String
    Shared g_FinYear As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            lblerror.Visible = False
            If (Not IsPostBack) Then        'If IsPostBack is false then
                'ddlCategory.SelectedValue = 0
                txtPagelink.Text = ""
                fillgrid()
                ' Fill_Combo("select category_Kid, category_Name from Category_Master where Category_ParentId='Root Category' and Category_IsDeleted = '0' order by category_name asc", ddlCategory)
                fillcombo1()
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub fillgrid()
        Try
            Dim con As New SqlConnection()

            con.ConnectionString = ConfigurationManager.AppSettings("ConnectionString")
            gvCategory.Columns(3).Visible = True
            Dim strQuery As String = "SELECT     distinct Category_Name, Category_Kid, Category_PageLink, Category_ParentId   FROM Category_Master WHERE     Category_ParentId = N'Root Category' OR     Category_ParentId = N'Root Categ'"
            Dim arrdatakey(0) As String
            arrdatakey(0) = "Category_Kid"
            'gvCity.DataKeyNames = arrdatakey
            Dim cmd As New SqlCommand(strQuery, con)
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            gvCategory.DataKeyNames = arrdatakey
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            gvCategory.DataSource = dr
            gvCategory.DataBind()

            gvCategory.Columns(3).Visible = False
            dr.Close()
            con.Close()
            cmd.Dispose()
            con.Dispose()
            dr = Nothing
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try

    End Sub

    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim con As New SqlConnection()
            Dim strproc = "Category_Page_Link_Proc"
            con.ConnectionString = ConfigurationManager.AppSettings("ConnectionString")
            Dim cmd As New SqlCommand(strproc, con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@Category_Kid", SqlDbType.NVarChar, 10).Value = gvCategory.SelectedValue
            cmd.Parameters.Add("@Category_Name", SqlDbType.NVarChar, 25).Value = ddlCategory.SelectedItem.Text
            cmd.Parameters.Add("@Category_PageLink", SqlDbType.NVarChar, 75).Value = "~/category/" & txtPagelink.Text.Trim
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            fillgrid()
            txtPagelink.Text = ""
            ddlCategory.SelectedValue = 0
            showmessage("Record updated successfully", lblerror)
            con.Dispose()
            cmd.Dispose()

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub gvCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            fillcombo1()

            ddlCategory.Items.FindByValue(Convert.ToString(gvCategory.SelectedValue).Trim).Selected = True
            txtPagelink.Text = gvCategory.SelectedRow.Cells(2).Text
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyval")
        End Try
    End Sub

    Protected Sub fillcombo1()
        Try
            Dim con As New SqlConnection()
            con.ConnectionString = ConfigurationManager.AppSettings("ConnectionString")
            Dim str As String = "select distinct category_Kid, category_Name from Category_Master where Category_ParentId='Root Category' and Category_IsDeleted = '0' order by category_name asc"
            Dim sda As New SqlDataAdapter(str, con)
            Dim ds As New DataSet()
            sda.Fill(ds)
            ddlCategory.DataTextField = "Category_Name"
            ddlCategory.DataValueField = "Category_Kid"

            ddlCategory.DataSource = ds
            ddlCategory.DataBind()
            ddlCategory.Items.Insert(0, New ListItem("Select", 0))
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    
    Protected Sub gvCategory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCategory.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim strpagelink As String = e.Row.Cells(3).Text
            strpagelink = strpagelink.Remove(0, 11)
            e.Row.Cells(2).Text = strpagelink
        End If
    End Sub
End Class
