B M Suchitra

B M Suchitra

  • NA
  • 507
  • 418k

GRIDVIEW COLUMN NOT GETTING UPDATED

Dec 17 2010 2:24 AM
Hi friends plzz go through this code and let me know whats the problem with this..Here the gridview column is not getting updated and even the delete button works but also gives and error... plzz help...
PAGE SOURCE IS:
<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%">
                    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                    <Columns>
                        <asp:TemplateField HeaderText="category id">
                        <ItemTemplate>
                        <asp:Label ID="lblcatid" runat="server" Text='<%#Eval("category_id") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       <asp:TemplateField HeaderText="category name">
                       <ItemTemplate>
                       <asp:TextBox ID="txtcategory" runat="server" Text='<%#Eval("category_name") %>'></asp:TextBox>
                       </ItemTemplate>
                       </asp:TemplateField>
                        <asp:TemplateField>
                        <EditItemTemplate>
                        <asp:LinkButton ID="lnkbtnUpdate" CausesValidation="true" CommandName="update" runat="server" Text="update"></asp:LinkButton>
                        <asp:LinkButton ID="lnkbtnCancel" runat="server" Text="cancel" CommandName="cancel" CausesValidation="true"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                        <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="true" CommandName="edit" Text="edit"></asp:LinkButton>
                        </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowDeleteButton="True" />
                    </Columns>
                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="#DCDCDC" />
                </asp:GridView>
               
CODE BEHIND IS:
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


Answers (5)