views:

1063

answers:

1

I have a Gridview that shows a list of Contacts via a sqlDataSource.

Included is an Item Template for Deleting a record - a LinkButton - with OnClientClick set to use some JavaScript: "return confirm('Are you sure you want to delete this user?');" This give the user a little pop up with a Yes or Cancel option.

When the user is deleted from the Contact table, I also need to delete the same user in the AspNet Membership tables using Membership.DeleteUser(UserName, true) statement.

The Contact.IntranetUserName column and UserName of the Membership tables contain the same data values, so that makes things convenient.

Not knowing what event to use, I started with using the Gridview's OnRowCommand - but can't figure out how to grab the value of the "IntranetUserName" column of the row that's being deleted (by the Item Template in Contact Table) and pass it to the Membership.DeleteUser statement.

Any C# ideas on this would be much apprecitated. Perhaps I should be using GridView's OnRowDeleted or OnRowDeleting instead? Or perhaps the LinkButtons OnCommand?

GridView and SqlDataSource here:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ContactID" 
                    DataSourceID="SqlDataSource2" Width="738px" OnRowCommand="deleteFromAspnetProvider" >
                    <Columns>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirm('Are you sure you want to delete this user?');" 
                                    CommandName="Delete" Text="Delete User"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="IntranetUserName" HeaderText="IntranetUserName" SortExpression="IntranetUserName" Visible="false"  />

                        <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                            SortExpression="FirstName" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                            SortExpression="LastName" />
                        <asp:BoundField DataField="CompanyName" HeaderText="Company" 
                            SortExpression="CompanyName" />
                        <asp:BoundField DataField="EmailAddress" HeaderText="Email Address" 
                            SortExpression="EmailAddress" />
                        <asp:BoundField DataField="UserLevelType" HeaderText="UserLevelType" 
                            SortExpression="UserLevelType" />
                    </Columns>
                </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DataConnect %>" 

    SelectCommand="SELECT * FROM [Contact] WHERE ([CompanyName] = @CompanyName)" 
    DeleteCommand="DELETE FROM [Contact] WHERE [ContactID] = @ContactID" 
    InsertCommand="INSERT INTO [Contact] ([UserId], [UserSID], [LocationID], [IntranetUserName], [FirstName], [LastName], [CompanyName], [EmailAddress], [ContactPhone], [ContactPhoneExt], [ContactFAX], [DateUpdated], [ActiveMember], [UserLevelType]) VALUES (@UserId, @UserSID, @LocationID, @IntranetUserName, @FirstName, @LastName, @CompanyName, @EmailAddress, @ContactPhone, @ContactPhoneExt, @ContactFAX, @DateUpdated, @ActiveMember, @UserLevelType)" 
    UpdateCommand="UPDATE [Contact] SET [UserId] = @UserId, [UserSID] = @UserSID, [LocationID] = @LocationID, [IntranetUserName] = @IntranetUserName, [FirstName] = @FirstName, [LastName] = @LastName, [CompanyName] = @CompanyName, [EmailAddress] = @EmailAddress, [ContactPhone] = @ContactPhone, [ContactPhoneExt] = @ContactPhoneExt, [ContactFAX] = @ContactFAX, [DateUpdated] = @DateUpdated, [ActiveMember] = @ActiveMember, [UserLevelType] = @UserLevelType WHERE [ContactID] = @ContactID">
    <SelectParameters>
        <asp:ControlParameter ControlID="memberCompanyNameLabel" Name="CompanyName" 
            PropertyName="Text" Type="String" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="ContactID" Type="Int32" />
        <asp:Parameter Name="IntranetUserName" Type="String" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="UserId" Type="Object" />
        <asp:Parameter Name="UserSID" Type="Int32" />
        <asp:Parameter Name="LocationID" Type="Int32" />
        <asp:Parameter Name="IntranetUserName" Type="String" />
        <asp:Parameter Name="FirstName" Type="String" />
        <asp:Parameter Name="LastName" Type="String" />
        <asp:Parameter Name="CompanyName" Type="String" />
        <asp:Parameter Name="EmailAddress" Type="String" />
        <asp:Parameter Name="ContactPhone" Type="String" />
        <asp:Parameter Name="ContactPhoneExt" Type="String" />
        <asp:Parameter Name="ContactFAX" Type="String" />
        <asp:Parameter Name="DateUpdated" Type="DateTime" />
        <asp:Parameter Name="ActiveMember" Type="Boolean" />
        <asp:Parameter Name="UserLevelType" Type="Int32" />
        <asp:Parameter Name="ContactID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="UserId" Type="Object" />
        <asp:Parameter Name="UserSID" Type="Int32" />
        <asp:Parameter Name="LocationID" Type="Int32" />
        <asp:Parameter Name="IntranetUserName" Type="String" />
        <asp:Parameter Name="FirstName" Type="String" />
        <asp:Parameter Name="LastName" Type="String" />
        <asp:Parameter Name="CompanyName" Type="String" />
        <asp:Parameter Name="EmailAddress" Type="String" />
        <asp:Parameter Name="ContactPhone" Type="String" />
        <asp:Parameter Name="ContactPhoneExt" Type="String" />
        <asp:Parameter Name="ContactFAX" Type="String" />
        <asp:Parameter Name="DateUpdated" Type="DateTime" />
        <asp:Parameter Name="ActiveMember" Type="Boolean" />
        <asp:Parameter Name="UserLevelType" Type="Int32" />
    </InsertParameters>
</asp:SqlDataSource>

+1  A: 

Try using the RowDeleting event instead.

void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  string name = e.Values["IntranetUserName"] as string;
}

PS: I don't have VS installed on this machine so I cant really test it myself... Sorry.

JohannesH
Thank you Johannes. I'm getting an error that Membership.DeleteUser cannont be a null value - so I think that RowDeleting isn't getting the e.Value in time before it gets deleted by the other event. Not sure if there are any other ways to capture that "IntranetUserName" before it gets deleted.Thanks for any further suggetions that you may have.
Doug
I suppose that since its on the delete event only "ContactID" will be channeled to the handler.
JohannesH
Yes - I think you're right. I've resolved this by using a details view (an extra step for the end user but it's working now).
Doug
Ok, I'm glad you figured it out.
JohannesH