Link to home
Start Free TrialLog in
Avatar of ferguson_jerald
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:


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.OnRowDeleting(GridViewDeleteEventArgs e) +1610277
   System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +621
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +974
   System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +205
   System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9671830
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Here's the gridview code from the aspx page:
<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>

Open in new window


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();
        }


    }
}

Open in new window


Any help would be greatly appreciated.

Thanks,
J
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ferguson_jerald
ferguson_jerald

ASKER

Thank you for such a quick response.  It works great now.