ferguson_jerald
asked on
Why do I get an error when I try to use the delete button on a gridview within my asp.net c# application?
Hello Experts.
I'm following the tutorial provided at http://www.asp.net/web-forms/overview/older-versions-security/roles/creating-and-managing-roles-cs for creating and managing roles in my asp.net c# application. I am able to add new items to the roles table, but I am unable to delete a role. I get the following error when I click on the delete button:
Here's the code behind:
Any help would be greatly appreciated.
Thanks,
J
I'm following the tutorial provided at http://www.asp.net/web-forms/overview/older-versions-security/roles/creating-and-managing-roles-cs for creating and managing roles in my asp.net c# application. I am able to add new items to the roles table, but I am unable to delete a role. I get the following error when I click on the delete button:
Here's the gridview code from the aspx page:
Server Error in '/' Application
The GridView 'RoleList' fired event RowDeleting which wasn't handled.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The GridView 'RoleList' fired event RowDeleting which wasn't handled.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The GridView 'RoleList' fired event RowDeleting which wasn't handled.]
System.Web.UI.WebControls.GridView.O nRowDeleti ng(GridVie wDeleteEve ntArgs e) +1610277
System.Web.UI.WebControls.GridView.H andleDelet e(GridView Row row, Int32 rowIndex) +621
System.Web.UI.WebControls.GridView.H andleEvent (EventArgs e, Boolean causesValidation, String validationGroup) +974
System.Web.UI.WebControls.GridView.R aisePostBa ckEvent(St ring eventArgument) +205
System.Web.UI.WebControls.GridView.S ystem.Web. UI.IPostBa ckEventHan dler.Raise PostBackEv ent(String eventArgument) +13
System.Web.UI.Page.RaisePostBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEven t(NameValu eCollectio n postData) +9671830
System.Web.UI.Page.ProcessRequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +1724
<asp:GridView ID="RoleList" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:CommandField DeleteText="Delete Role" ShowDeleteButton="True"/>
<asp:TemplateField HeaderText="Role">
<ItemTemplate>
<asp:Label runat="server" ID="RoleNameLabel" Text='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
namespace twia_cpc.CPC_Admin
{
public partial class ManageRoles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
DisplayRolesInGrid();
}
private void DisplayRolesInGrid()
{
RoleList.DataSource = Roles.GetAllRoles();
RoleList.DataBind();
}
protected void CreateRoleButton_Click(object sender, EventArgs e)
{
string newRoleName = RoleName.Text.Trim();
if (!Roles.RoleExists(newRoleName))
{
// Create the role
Roles.CreateRole(newRoleName);
// Refresh the RoleList Grid
DisplayRolesInGrid();
}
RoleName.Text = string.Empty;
}
protected void RoleList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Get the RoleNameLabel
Label RoleNameLabel = RoleList.Rows[e.RowIndex].FindControl("RoleNameLabel") as Label;
// Delete the role
Roles.DeleteRole(RoleNameLabel.Text, false);
// Rebind the data to the RoleList grid
DisplayRolesInGrid();
}
}
}
Any help would be greatly appreciated.
Thanks,
J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER