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
ferguson_jeraldAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

You need to add the following event to the GridView.

<asp:GridView ID="RoleList" runat="server" AutoGenerateColumns="False"  OnRowDeleting="RoleList_RowDeleting">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ferguson_jeraldAuthor Commented:
Thank you for such a quick response.  It works great now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.