Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
.ASPX SOURCE
XML
<asp:GridView ID="gvcategory" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="gvcategory_PageIndexChanging"
OnRowCancelingEdit="gvcategory_RowCancelingEdit" OnRowDeleting="gvcategory_RowDeleting" OnRowEditing="gvcategory_RowEditing"
OnRowUpdating="gvcategory_RowUpdating" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" AllowPaging="True" Width="50%" >
  <asp:TemplateField HeaderText="category id">
  <asp:Label ID="lblcatid" runat="server" Text='<%#Eval("category_id") %>'>
  <asp:TemplateField HeaderText="category name">
  <asp:TextBox ID="txtcategory" runat="server" Text='<%#Eval("category_name") %>'>
  <asp:TemplateField>
  <asp:LinkButton ID="lnkbtnUpdate" CausesValidation="true" CommandName="update" runat="server" Text="update">
  <asp:LinkButton ID="lnkbtnCancel" runat="server" Text="cancel" CommandName="cancel" CausesValidation="true">
  <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="true" CommandName="edit" Text="edit">
   <asp:CommandField ShowDeleteButton="True" />
  <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />


====================================================
CODE BEHIND

VB
Imports System.Data.SqlClient
Imports System.Data
Partial Class management_add_category
    Inherits System.Web.UI.Page
    Dim obj As New data
    Dim da As SqlDataAdapter
    Dim ds As DataSet
    Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnadd.Click
        obj.Connect()
        obj.cmd.CommandText = "insert into category_master(category_name)values('" & txtcategory.Text & "')"
        obj.cmd.ExecuteNonQuery()
        MsgBox("category added!", MsgBoxStyle.Information, "Mohire`s")
        txtcategory.Text = ""
        obj.db.Close()
        fill_data()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("user_id") = "" Then
            Response.Redirect("admin_login.aspx")
        End If
        fill_data()
    End Sub

    Private Sub fill_data()

        obj.Connect()
        obj.cmd.CommandText = "Select * from category_master order by category_id asc"
        da = New SqlDataAdapter(obj.cmd)
        ds = New DataSet
        da.Fill(ds, "category_master")
        gvcategory.DataSource = ds.Tables("category_master")
        gvcategory.DataBind()
        obj.db.Close()

    End Sub

    Protected Sub gvcategory_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvcategory.PageIndexChanging
        gvcategory.PageIndex = e.NewPageIndex
        fill_data()
    End Sub

    Protected Sub gvcategory_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvcategory.RowCancelingEdit
        gvcategory.EditIndex = -1
        fill_data()
    End Sub

    Protected Sub gvcategory_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvcategory.RowDeleting
        Dim l1 As Label = CType(gvcategory.Rows(e.RowIndex).FindControl("lblcatid"), Label)
        obj.Connect()
        obj.cmd.CommandText = "delete category_master where category_id=" & l1.Text & ""
        obj.cmd.ExecuteNonQuery()
        gvcategory.EditIndex = -1
        MsgBox("category deleted!", MsgBoxStyle.Information, "Mohire`s")
        obj.db.Close()

        fill_data()

    End Sub

    Protected Sub gvcategory_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvcategory.RowEditing
        gvcategory.EditIndex = e.NewEditIndex
        fill_data()
    End Sub

    Protected Sub gvcategory_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvcategory.RowUpdating
        Try
            Dim l1 As Label = CType(gvcategory.Rows(e.RowIndex).FindControl("lblcatid"), Label)
            Dim txtcatname As TextBox = CType(gvcategory.Rows(e.RowIndex).FindControl("txtcategory"), TextBox)

            obj.Connect()
            obj.cmd.CommandText = "update category_master set category_name='" & txtcatname.Text & "' where category_id=" & l1.Text
            obj.cmd.ExecuteNonQuery()
            gvcategory.EditIndex = -1
            MsgBox("category updated!", MsgBoxStyle.Information, "Mohire`s")
            obj.db.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        fill_data()
    End Sub
End Class



[
Posted
Updated 16-Dec-10 23:12pm
v4
Comments
JF2015 17-Dec-10 4:46am    
Edited to correct code formatting. Please don't shout - please use correct upper and lower case when posting on CodeProject.
Sandeep Mewara 17-Dec-10 5:23am    
1. Post specfifc code.
2. What do you mean 'data is not getting updated'?
Abdul Quader Mamun 17-Dec-10 5:46am    
What is the question?
su_ma2010 18-Dec-10 0:07am    
To admin i was totally not aware so i thought using caps will highlight my question.. sorry

1 solution

In the page load, put fill_data into Not IsPostBack condition, what is happening in your case is every time the page is loaded, the grid is loaded with the data and all the previous action on the grid is cleared.

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
If Session("user_id") = "" Then
            Response.Redirect("admin_login.aspx")        
End If
If Not IsPostBack Then
fill_data()
End If
End Sub
 
Share this answer
 
Comments
su_ma2010 17-Dec-10 22:10pm    
Thank you very much sir.... You ans helped me to correct the code...
Sir for the delete event in gridview i am getting this exception:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index....... plzz help me
su_ma2010 17-Dec-10 22:11pm    
The code for delete event in gridview is as below:

Protected Sub gvcategory_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvcategory.RowDeleting
Dim l1 As Label = CType(gvcategory.Rows(e.RowIndex).FindControl("lblcatid"), Label)
obj.Connect()
obj.cmd.CommandText = "delete category_master where category_id=" & l1.Text & ""
obj.cmd.ExecuteNonQuery()
gvcategory.EditIndex = -1
MsgBox("category deleted!", MsgBoxStyle.Information, "Mohire`s")
obj.db.Close()

fill_data()

End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900