locked
Delete user from membership problem RRS feed

  • Question

  • User-1767698477 posted

    I'm displaying users in a gridview with a delete button. I'm using the new keyword to generate a new instance. Why do I get an Object reference not set to an instance of an object message?

    Public Sub DeleteCommand(sender As Object, e As CommandEventArgs)
    Dim username As String = (e.CommandArgument.ToString)
    Dim mu As New SqlMembershipProvider 
    mu.DeleteUser(username, True) <- Object reference not set to an instance of an object.
    GridView1.Data

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataKeyNames="UserId" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="UserId" HeaderText="UserId" ReadOnly="True" SortExpression="UserId" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="PasswordQuestion" HeaderText="PasswordQuestion" SortExpression="PasswordQuestion" />
                <asp:CheckBoxField DataField="IsApproved" HeaderText="IsApproved" SortExpression="IsApproved" />
                <asp:TemplateField HeaderText="UserName" SortExpression="UserName">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="Deletecommand" CommandName="Deleteuser"
                            Text="Delete user" CommandArgument='<%# Bind("Username") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


    Wednesday, May 13, 2020 5:13 PM

Answers

  • User753101303 posted

    Hi,

    AFAIK it is not supposed to be used directly and more likely it's because it's not initialized property. Instead use MemberShip.DeleteUser(username,True) as shown at:
    https://docs.microsoft.com/en-us/dotnet/api/system.web.security.membership.deleteuser?view=netframework-4.8 (and scroll down to samples)

    The idea is that the actual provider is configured in the web.config file and MemberShip.DeleteUser will forward this call to the configured provider. As a result you can use the exact same code regardless of which membership provider is used behind the scene.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2020 6:42 PM

All replies

  • User753101303 posted

    Hi,

    AFAIK it is not supposed to be used directly and more likely it's because it's not initialized property. Instead use MemberShip.DeleteUser(username,True) as shown at:
    https://docs.microsoft.com/en-us/dotnet/api/system.web.security.membership.deleteuser?view=netframework-4.8 (and scroll down to samples)

    The idea is that the actual provider is configured in the web.config file and MemberShip.DeleteUser will forward this call to the configured provider. As a result you can use the exact same code regardless of which membership provider is used behind the scene.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2020 6:42 PM
  • User-1767698477 posted

    I have resolved my issue:

    Dim username As String = (e.CommandArgument.ToString)
    Membership.DeleteUser(username, True)
    GridView1.DataBind()

    Wednesday, May 13, 2020 7:49 PM