Bineesh  Viswanath

Bineesh Viswanath

  • NA
  • 1k
  • 777.8k

Row_Deleting Event Error in ASP.NET

Jan 3 2014 5:58 AM
Sir, I having a error while I try to Delete a Row from GridView in ASP.NET.
 
 Nothing fire when click in the Delete button, which is a LinkButton . This is how the GridView look like:-
 
 
Nothing happening when I click on the Edit and Delete LinkButtons . Here the HTML Code for this GridView :-
 
<Columns>
<asp:TemplateField HeaderText="S.I.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class Id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblClassId" runat="server" Text='<%#Eval("classId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblAdd" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class Name">
<ItemTemplate>
<asp:Label ID="lblClassName" runat="server" Text='<%#Eval("className") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtClassName" runat="server" Text='<%#Eval("className") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddClassName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Seats">
<ItemTemplate>
<asp:Label ID="lblNoOfSeats" runat="server" Text='<%#Eval("noOfSeats") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNoOfSeats" runat="server" Text='<%#Eval("noOfSeats") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddNoOfseats" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" Text="Edit" CommandName="Edit" runat="server">Edit</asp:LinkButton>
<br />
<span onclick="return confirm('Are you sure want to Delete this Record?')">
<asp:LinkButton ID="lnkBtnDelete" Text="Delete" CommandName="Delete" runat="server">Delete</asp:LinkButton></span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" Text="Update" CommandName="Update" runat="server">Update</asp:LinkButton>
<br />
<asp:LinkButton ID="lnkBtnCancel" Text="Cancel" CommandName="Cancel" runat="server">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddRecord" CommandName="Addd" runat="server" Text="Save" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
 
And the C# code to handle the process:-
 
protected void gvClass_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvClass.PageIndex = e.NewPageIndex;
BindGridView();
}

protected void gvClass_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvClass.EditIndex = -1;
BindGridView();
}

protected void gvClass_RowEditing(object sender, GridViewEditEventArgs e)
{
gvClass.EditIndex = e.NewEditIndex;
BindGridView();
}

protected void gvClass_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

Label lblClass = (Label)gvClass.Rows[e.RowIndex].FindControl("lblClassId");
string classId = lblClass.Text;
classSP spClass = new classSP();
int inCount = spClass.ClassDelete(classId);
if (inCount > 0)
{
       HttpContext.Current.Response.Write("<script>alert('Data Deleted Succefully')</script>");
}
gvClass.EditIndex = -1;
BindGridView();
 
classSP Code:-
 
SqlCommand cmd = new SqlCommand("ClassDelete", sqlCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("classId", SqlDbType.Int).Value = Convert.ToInt32(strClassId.ToString());
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
DataSet dSet = new DataSet();
sqlDa.Fill(dSet);
return Convert.ToInt32(strClassId); 

Answers (2)