Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm using the Membership.DeleteUser(User.Identity.Name); to delete a member from my database. I'm using a Gridview to display multiple members and their roles. The issue I'm having is whenever I click the delete button which is activated by a: OnRowDeleting="gvwUsers_RowDeleting" method... It deletes the member that I'm signed in as, NOT the member I selected in the Gridview. How do I change the code to delete the member I select in the Gridview that has a delete button in the same column of the member I'm trying to delete instead of the member I'm signed in as?

What I have tried:

ASP.NET
<asp:GridView ID="gridUserAccounts" runat="server" CssClass="GridMain" AutoGenerateColumns="False"  DataSourceID="UserAccounts_DS"
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" GridLines="None" OnRowDeleting="gvwUsers_RowDeleting">
            <alternatingrowstyle backcolor="#ffffff" />
            <rowstyle cssclass="GridRow" />
            <HeaderStyle CssClass="GridHeader" />
            <footerstyle cssclass="GridHeader" />
            <pagerstyle forecolor="#ffffff" backcolor="#003366" />
            <columns>
            <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName"/>
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            <asp:BoundField DataField="RoleName" HeaderText="RoleName" SortExpression="RoleName" />
            </columns>

CODE BEHIND:
C#
protected void gvwUsers_RowDeleting(object sender, EventArgs e)
   {
       Membership.DeleteUser(User.Identity.Name, true);
   }

Any help would be appreciated..
Posted
Updated 6-Jun-16 9:18am
v2

1 solution

You've told the membership system to delete the user you're logged in as. You therefore shouldn't be surprised when it deletes the user you're logged in as! :)

To delete the member selected in the GridView, you need to pass the username for that user to the DeleteUser method instead.

Start by setting the DataKeyNames property[^] on the GridView:
ASP.NET
<asp:GridView ... DataKeyNames="UserName">

Then use the correct EventArgs type[^] for the RowDeleting event handler[^]:
C#
protected void gvwUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    ...
}

Then, you will be able to retrieve the key of the row being deleted, and pass that to the DeleteUser method:
C#
protected void gvwUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string userToDelete = (string)e.Keys["UserName"];
    Membership.DeleteUser(userToDelete, true);
}
 
Share this answer
 
Comments
Member 9320578 6-Jun-16 19:28pm    
Thank you! I had to tweak it a little bit, but it works because of your answer. Thank you!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900