Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.IO
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Class Admin_product_index_master
    Inherits System.Web.UI.Page
    Dim str, str1 As String
   
    Dim strcon As String = ConfigurationSettings.AppSettings.Item("ConnectionString")
    Dim con As New SqlConnection(strcon)
    Dim cmd As New SqlCommand()
    Dim dr As SqlDataReader
    Dim Category(10) As String
    Dim i As Integer
    Dim CatID1 As String
    Dim CatID2 As String
    Dim CatID3 As String

    Dim sproduct As Integer
    Dim dt1 As DataTable
    Dim categories As DataTable
    Dim count As Integer
    Dim parentid As String
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        'Put user code to initialize the page here
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
          
            If Not Me.Page.IsPostBack Then
                If Session("AdminID") = Nothing Then
                    Response.Redirect("AdminLogin.aspx")
                End If
                ' gvProducts.Visible = False
                '------getting the value of subcatid from the getparents function that declared in this form--------

                btnUpdate.Visible = True

                sproduct = 0
                g_FinYear = Session("g_FinYear").ToString()
                g_Company = Session("g_Company").ToString()
                g_User = Session("g_User").ToString()
                parentid = Request.QueryString("subcatid")
                If parentid = Nothing Or parentid = "Root" Then
                    parentid = "Root Category"
                Else
                    parentid = Request.QueryString("subcatid")
                End If
                i = 0
                LoadCategory(parentid)
                Session("c") = Nothing
                cbosubCategory1.Visible = False
                cbosubCategory2.Visible = False

            End If
            txtIndex.Enabled = False
            txtIndex2.Enabled = False
            txtIndex3.Enabled = False
            cboCategory.Visible = True
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.SelectedIndexChanged
        ' gvProducts.Visible = False
        Try
            If cboCategory.SelectedItem.Text = "Select Category" Then
                ' Panel1.Visible = True
                Label2.Visible = True
                Label2.Text = "Please Select the Category"
                '  Spcat.InnerText = cboCategory.SelectedItem.Text + " > "
                '  SpSubCat1.Visible = False
                ''extrs code
                cbosubCategory1.Visible = False
                cbosubCategory2.Visible = False
                Label15.Visible = False
            Else
                Session("lastsubcategory") = ""
                Session("lastsubcategory") = cboCategory.SelectedItem.Value
                CatID1 = RemoveLiterals(cboCategory.SelectedItem.Value)
                CatID2 = ""
                CatID3 = ""
                getparents(cboCategory.SelectedItem.Value)
                LoadSubCategory1(cboCategory.SelectedItem.Value)
                getdesc(RemoveLiterals(cboCategory.SelectedItem.Value))
                Session("c") = RemoveLiterals(cboCategory.SelectedItem.Value)
            End If
            If cboCategory.SelectedIndex = 0 Then
                ' setbutton(False)
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Protected Sub getdesc(ByVal catid As String)
        Try
            txtIndex.Text = ""
            txtIndex2.Text = ""
            txtIndex3.Text = ""

            btnUpdate.Visible = True

            Dim dt As DataTable = ReturnDataTable("select CategoryIndex_Name from Category_Index_Master where CategoryIndex_IsDeleted='0' and Category_Kid='" & catid & "'")

            'Dim strqry As String = "select CategoryIndex_Name from Category_Index_Master where Category_IsDeleted='0' and Category_Kid='" & catid & "'"
            '  Dim con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
            ' Dim cmd As New SqlCommand(strqry, con)
            ' If con.State = ConnectionState.Open Then
            ' con.Close()
            ' End If
            'con.Open()
            ' Dim desc As String = Convert.ToString(cmd.ExecuteScalar)
            '  con.Close()
            If dt.Rows.Count = 1 Then
                txtIndex.Text = dt.Rows(0).Item(0).ToString
            ElseIf dt.Rows.Count = 2 Then
                txtIndex.Text = dt.Rows(0).Item(0).ToString
                txtIndex2.Text = dt.Rows(1).Item(0).ToString
            ElseIf dt.Rows.Count = 3 Then
                txtIndex.Text = dt.Rows(0).Item(0).ToString
                txtIndex2.Text = dt.Rows(1).Item(0).ToString
                txtIndex3.Text = dt.Rows(2).Item(0).ToString
            End If

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub


    Private Sub cbosubCategory1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbosubCategory1.SelectedIndexChanged
        Try
            ' gvProducts.Visible = False
            Session("lastsubcategory") = cbosubCategory1.SelectedItem.Value
            CatID2 = RemoveLiterals(cbosubCategory1.SelectedItem.Value)
            CatID1 = ""
            CatID3 = ""
            getparents(RemoveLiterals(cbosubCategory1.SelectedItem.Value))
            LoadSubCategory2(RemoveLiterals(cbosubCategory1.SelectedItem.Value))
            '   SpSubCat.InnerText = cbosubCategory1.SelectedItem.Text + " > "
            '  Session("subcategory1") = SpSubCat.InnerText
            'Response.Write(Session("subcategory1"))
            '   SpSubCat.Visible = True
            '   SpSubCat1.Visible = True
            ''extra code
            cbosubCategory1.Visible = True
            ''cbosubCategory2.Visible = True
            '   SpSubCat1.InnerText = "Select Sub Category"

            getdesc(RemoveLiterals(cbosubCategory1.SelectedItem.Value))
            Session("c") = RemoveLiterals(cbosubCategory1.SelectedItem.Value)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Private Sub cbosubCategory2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbosubCategory2.SelectedIndexChanged
        Try
            'gvProducts.Visible = False
            Session("lastsubcategory") = RemoveLiterals(cbosubCategory2.SelectedItem.Value)
            CatID3 = RemoveLiterals(cbosubCategory2.SelectedItem.Value)
            CatID1 = ""
            CatID2 = ""
            getparents(RemoveLiterals(cbosubCategory2.SelectedItem.Value))
            LoadSubCategory3(RemoveLiterals(cbosubCategory2.SelectedItem.Value))

            getdesc(RemoveLiterals(cbosubCategory2.SelectedItem.Value))
            Session("c") = RemoveLiterals(cbosubCategory2.SelectedItem.Value)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Private Sub LoadSubCategory3(ByVal intId As String)
        Try
            Dim strsql As String
            'strsql = "select cat_id as id,catname as Name from categorymaster where parent_id=" & intId & "order by catname asc"
            strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' and Category_Isdeleted='0' ORDER BY Category_Name"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()

            Dim cmd As New SqlCommand(strsql, con)
            Dim mReader As SqlDataReader
            mReader = cmd.ExecuteReader
            cboCategory.Items.Clear()
            cboCategory.Items.Add(New ListItem("Select Subcategory", "-4"))
            If mReader.Read Then
                cboCategory.Items.Add(New ListItem(mReader("Name"), mReader("id")))
                While mReader.Read
                    cboCategory.Items.Add(New ListItem(mReader("Name"), mReader("id")))
                End While
                Label14.Visible = True
                Label15.Visible = False
                Label16.Visible = False
                cbosubCategory1.Visible = False
                cbosubCategory2.Visible = False
                cboCategory.ForeColor = Drawing.Color.Red
                cbosubCategory1.ForeColor = Drawing.Color.Black
                cbosubCategory2.ForeColor = Drawing.Color.Black
                Label14.Text = "Select the next level"
            Else
                Label14.Visible = False
                Label15.Visible = False
                Label16.Visible = False
                Label2.Visible = True
                Label2.Text = "Last category selected."
                txtIndex.Enabled = True
                txtIndex2.Enabled = True
                txtIndex3.Enabled = True
                Session("CategoryId") = intId

                'Response.Redirect("Product_Information.aspx?CatID=" & intId)
            End If
            mReader.Close()

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Private Sub LoadCategory(ByVal parentid As String)
        Try
            Dim strsql As String
            'strsql = "select cat_id as id,catname as Name from categorymaster where parent_id=" & parentid & "order by catname asc"
            strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & parentid & "' and Category_Isdeleted='0' ORDER BY Category_Name"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(strsql, con)
            Dim mReader As SqlDataReader
            mReader = cmd.ExecuteReader
            cboCategory.Items.Clear()
            cboCategory.Items.Add(New ListItem("Select Category", "-1"))
            If mReader.Read Then
                cboCategory.Items.Add(New ListItem(mReader("Name"), RemoveLiterals(mReader("id"))))
                While mReader.Read
                    cboCategory.Items.Add(New ListItem(mReader("Name"), RemoveLiterals(mReader("id"))))
                End While
                Label14.Visible = True
                Label15.Visible = False
                Label16.Visible = False

                cboCategory.ForeColor = Drawing.Color.Red
                cbosubCategory1.ForeColor = Drawing.Color.Black
                cbosubCategory2.ForeColor = Drawing.Color.Black

                Label14.Text = "Select the category"
            Else
                Label14.Visible = False
                Label15.Visible = False
                Label16.Visible = False
                Label2.Visible = True
                Label2.Text = "Last category selected."
                txtIndex.Enabled = True
                txtIndex2.Enabled = True
                txtIndex3.Enabled = True

                Session("CategoryId") = parentid

                'Response.Redirect("Product_Information.aspx?CatID =" & parentid)
            End If
            mReader.Close()


        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Private Sub LoadSubCategory1(ByVal intId As String)
        Try
            Dim strsql As String
            'strsql = "Select cat_id as id,catname as Name from categorymaster where parent_id=" & intId & "order by catname asc"
            strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' and Category_Isdeleted='0' ORDER BY Category_Name"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()

            Dim cmd As New SqlCommand(strsql, con)
            Dim mReader As SqlDataReader
            mReader = cmd.ExecuteReader
            cbosubCategory1.Items.Clear()
            cbosubCategory1.Items.Add(New ListItem("Select Subcategory", "-2"))
            If mReader.Read Then
                cbosubCategory1.Items.Add(New ListItem(mReader("Name"), RemoveLiterals(mReader("id"))))
                While mReader.Read
                    cbosubCategory1.Items.Add(New ListItem(mReader("Name"), RemoveLiterals(mReader("id"))))
                End While
                Label14.Visible = False
                Label15.Visible = True
                Label16.Visible = False
                cboCategory.ForeColor = Drawing.Color.Black
                cbosubCategory1.ForeColor = Drawing.Color.Red
                cbosubCategory2.ForeColor = Drawing.Color.Black
                cbosubCategory1.Visible = True
                cbosubCategory2.Visible = False
                Label15.Text = "Select the next level"
            Else
                'Response.Write("check")
                cbosubCategory1.Visible = False
                cbosubCategory2.Visible = False
                Label14.Visible = False
                Label15.Visible = False
                Label16.Visible = False
                Label2.Visible = True
                Label2.Text = "Last category selected."
                txtIndex.Enabled = True
                txtIndex2.Enabled = True
                txtIndex3.Enabled = True

                Session("CategoryId") = intId

                'Response.Redirect("Product_Information.aspx?CatID =" & intId)
            End If
            mReader.Close()

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
    Private Sub LoadSubCategory2(ByVal intId As String)
        Try
            Dim strsql As String
            'strsql = "select cat_id as id,catname as Name from categorymaster where parent_id=" & intId & "order by catname asc"
            strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' and Category_Isdeleted='0' ORDER BY Category_Name"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()

            Dim cmd As New SqlCommand(strsql, con)
            Dim mReader As SqlDataReader
            mReader = cmd.ExecuteReader
            cbosubCategory2.Items.Clear()
            cbosubCategory2.Items.Add(New ListItem("Select Subcategory", "-3"))
            If mReader.Read Then
                cbosubCategory2.Items.Add(New ListItem(mReader("Name"), RemoveLiterals(mReader("id"))))
                While mReader.Read
                    cbosubCategory2.Items.Add(New ListItem(mReader("Name"), RemoveLiterals(mReader("id"))))
                End While
                Label14.Visible = False
                Label15.Visible = False
                Label16.Visible = True
                cboCategory.ForeColor = Drawing.Color.Black
                cbosubCategory1.ForeColor = Drawing.Color.Black
                cbosubCategory2.ForeColor = Drawing.Color.Red
                Label16.Text = "Select the next level"
                cbosubCategory2.Visible = True
            Else
                cbosubCategory2.Visible = False
                Label14.Visible = False
                Label15.Visible = False
                Label16.Visible = False
                Label2.Visible = True
                Label2.Text = "Last category selected."
                txtIndex.Enabled = True
                txtIndex2.Enabled = True
                txtIndex3.Enabled = True

                Session("CategoryId") = intId

                'Response.Redirect("Product_Information.aspx?CatID =" & intId)
            End If
            mReader.Close()

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try

    End Sub
    Private Function getparents(ByVal i As String) As String
        Try
            Dim strsql As String
            'strsql = "select parent_id ,catname as Name from categorymaster where cat_id=" & i
            strsql = "SELECT Category_ParentId, Category_Name FROM Category_Master WHERE Category_Kid = '" & i & "'"
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim cmd As New SqlCommand(strsql, con)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader
            Dim j As String
            Dim flag As Boolean = False
            If dr.Read Then
                j = dr(0)
                'Response.Write(j)
                flag = True
            Else
                'Response.Write("not")
            End If

            If flag = True Then
                ''If j = 0 Then
                ''Exit Function
                ''ElseIf j <> 0 Then
                txtIndex.Text = ""
                txtIndex2.Text = ""
                txtIndex3.Text = ""

                str1 = dr(1) & " > " & str1


                'str = "<a href=../ProductDetails/prodinfo.aspx?subcatid=" & dr(0) & ">" & dr(1) & "</a>" & " > " & str
                str = "<a href=./product-index-master.aspx?subcatid=" & dr(0) & ">" & dr(1) & "</a>" & " > " & str
                getparents(j)
                ''End If
            End If
            dr.Close()
            con.Close()
            Label13.Text = str
            Session("categorylevel") = Nothing
            Session("categorylevel") = str1
            Session("Category") = str1

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Function
    Public Sub FillCategoryName(ByVal Catid As String)
        Try
            Dim dt As DataTable
            dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0' ") 'and Category_CompanyId ='" & g_Company & "'
            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
                    '  lblError.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())
            End If
            
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try

    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            If txtIndex.Text.Trim = "" Then
                lblerror.Visible = True
                lblerror.Text = "Please Enter Index"
                Exit Sub
            End If

            If txtIndex.Text.Trim = txtIndex2.Text.Trim Then
                lblerror.Visible = True
                lblerror.Text = "Please Enter Different Index."
                txtIndex.Enabled = True
                txtIndex2.Enabled = True
                txtIndex3.Enabled = True
                Exit Sub
            End If

            If txtIndex2.Text.Trim <> "" And txtIndex3.Text.Trim <> "" Then
                If (txtIndex2.Text.Trim = txtIndex3.Text.Trim) Or txtIndex2.Text.Trim = "" Or txtIndex3.Text.Trim = "" Then
                    lblerror.Visible = True
                    lblerror.Text = "Please Enter Different Index."
                    txtIndex.Enabled = True
                    txtIndex2.Enabled = True
                    txtIndex3.Enabled = True
                    Exit Sub

                End If
            End If

            If txtIndex.Text.Trim <> "" And txtIndex3.Text.Trim <> "" Then
                If (txtIndex.Text.Trim = txtIndex3.Text.Trim) Or txtIndex.Text.Trim = "" Or txtIndex3.Text.Trim = "" Then

                    lblerror.Visible = True
                    lblerror.Text = "Please Enter Different Index."
                    txtIndex.Enabled = True
                    txtIndex2.Enabled = True
                    txtIndex3.Enabled = True
                    Exit Sub

                End If
            End If

            

            If cboCategory.SelectedItem.Text = "Select Category" Then
                lblerror.Visible = True
                lblerror.Text = "Please Select Category Properly"
                cboCategory.Focus()
                Exit Sub
            End If

            If Session("c") = Nothing Then
                lblerror.Visible = True
                lblerror.Text = "Please Select Product Properly"
                Exit Sub
            End If

            Dim str As String = ReturnValue("select category_kid from category_master where category_kid ='" & Session("c") & "'")
            If str = "" Then
                lblerror.Visible = True
                lblerror.Text = "Please Select Product Properly"
                Exit Sub
            End If

            If insertdata("update", Session("c")) = True Then
                showmessage("Record Saved sucessfully", lblerror)
                txtIndex.Text = ""
                txtIndex2.Text = ""
                txtIndex3.Text = ""

                If parentid = Nothing Or parentid = "Root" Then
                    parentid = "Root Category"
                Else
                    parentid = Request.QueryString("subcatid")
                End If
                LoadCategory(parentid)
                cbosubCategory1.Visible = False
                cbosubCategory2.Visible = False
                cboCategory.Visible = True

            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
   
    
    Protected Function insertdata(ByVal mode As String, ByVal catid As String) As Boolean
        Dim flag As Boolean = False
        Try

            Dim dt As DataTable = ReturnDataTable("select CategoryIndex_Kid,CategoryIndex_Name from Category_Index_Master where CategoryIndex_IsDeleted='0' and Category_Kid='" & catid & "'")
            If dt.Rows.Count <> 0 Then
                If dt.Rows.Count = 1 Then
                    If (txtIndex.Text.Trim <> "") Then
                        update_data(txtIndex.Text, dt.Rows(0).Item(0).ToString)
                    Else
                        delete_data(dt.Rows(0).Item(0).ToString)
                    End If

                    If (txtIndex2.Text.Trim <> "") Then
                        insert_data(txtIndex2.Text, catid)
                    End If
                    If (txtIndex3.Text.Trim <> "") Then
                        insert_data(txtIndex3.Text, catid)
                    End If
                    Exit Try
                End If

                If dt.Rows.Count = 2 Then
                    If (txtIndex.Text.Trim <> "") Then
                        update_data(txtIndex.Text, dt.Rows(0).Item(0).ToString)
                    Else
                        delete_data(dt.Rows(0).Item(0).ToString)
                    End If

                    If (txtIndex2.Text.Trim <> "") Then
                        update_data(txtIndex2.Text, dt.Rows(1).Item(0).ToString)
                    Else
                        delete_data(dt.Rows(1).Item(0).ToString)
                    End If

                    If (txtIndex3.Text.Trim <> "") Then
                        insert_data(txtIndex3.Text, catid)
                    End If
                    Exit Try
                End If
                If dt.Rows.Count = 3 Then
                    If (txtIndex.Text.Trim <> "") Then
                        update_data(txtIndex.Text, dt.Rows(0).Item(0).ToString)
                    Else
                        delete_data(dt.Rows(0).Item(0).ToString)
                    End If

                    If (txtIndex2.Text.Trim <> "") Then
                        update_data(txtIndex2.Text, dt.Rows(1).Item(0).ToString)
                    Else
                        delete_data(dt.Rows(1).Item(0).ToString)
                    End If

                    If (txtIndex3.Text.Trim <> "") Then
                        update_data(txtIndex3.Text, dt.Rows(2).Item(0).ToString)
                    Else
                        delete_data(dt.Rows(2).Item(0).ToString)
                    End If
                    Exit Try
                End If
            End If

            If (txtIndex.Text.Trim <> "") Then
                insert_data(txtIndex.Text, catid)
            End If

            If (txtIndex2.Text.Trim <> "") Then
                insert_data(txtIndex2.Text, catid)
            End If

            If (txtIndex3.Text.Trim <> "") Then
                insert_data(txtIndex3.Text, catid)
            End If

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
        flag = True
        Return flag

    End Function
    Protected Sub delete_data(ByVal ind_kid As String)
        Dim delstr As String
        delstr = "delete from Category_Index_Master where CategoryIndex_Kid='" & ind_kid & "'"
        Dim mycmd As New SqlCommand(delstr, con)
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        mycmd.ExecuteNonQuery()
    End Sub

    Protected Sub update_data(ByVal ind_name As String, ByVal ind_kid As String)
        Dim updatestr As String
        updatestr = "update Category_Index_Master set CategoryIndex_Name='" & ind_name.Trim & "' where CategoryIndex_Kid='" & ind_kid & "'"
        Dim mycmd As New SqlCommand(updatestr, con)
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        mycmd.ExecuteNonQuery()

    End Sub

    Protected Sub insert_data(ByVal ind_name As String, ByVal catid As String)
        Dim mykid As String
        Dim ins_str As String
        mykid = RemoveLiterals(NewPrimaryKey(Me))
        ins_str = "insert into Category_Index_Master values('" & mykid.ToString & "','" & ind_name.Trim & "','" & catid & "','" & "0" & "')"
        Dim mycmd As New SqlCommand(ins_str, con)
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        mycmd.ExecuteNonQuery()
    End Sub

    Protected Function returnintegervalue(ByVal strqry As String) As Integer
        Try
            Dim con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
            Dim cmd As New SqlCommand(strqry, con)
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            Dim i As Integer = Convert.ToInt32(cmd.ExecuteScalar())
            con.Close()
            Return i
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Function

    Protected Sub GenerateCode(ByRef code As String)
        Try
            Dim i As String
            Dim qry As String

            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "GDC" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from categorydescription where categorydescription_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    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 btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        txtIndex.Text = ""
        txtIndex2.Text = ""
        txtIndex3.Text = ""
        txtIndex.Focus()
    End Sub
End Class

