Solved

Gridview Delete multiple rows

Posted on 2010-08-31
5
596 Views
Last Modified: 2013-11-27
I have added functionality to my web page to allow users to delete multiple rows in a gridview at once.  I had the functionality working but it has stopped working and I'm not sure why.  Could someone look at my code and tell me if I'm missing something?  I have included my gridview source code and the vb code for the delete functionality.


Protected Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click

        ' Looping through all the rows in the GridView

        For Each row As GridViewRow In gvProfiles.Rows

            Dim username As String = Page.User.Identity.Name.Split("\"c)(1).ToString()

            Dim oConn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("Marketing_ResumesConnectionString").ConnectionString)
            Dim sql As New SqlCommand("spResumeProfileDeletion")
            sql.Connection = oConn
            sql.CommandType = CommandType.StoredProcedure
            Dim checkbox As CheckBox = CType(row.FindControl("chkdelete"), CheckBox)
            Dim profile As String = CType(row.FindControl("lblprofile"), Label).Text

            'Check if the checkbox is checked.


            If checkbox.Checked Then

                ' Retreive parameters to delete


                sql.Parameters.Add(New SqlParameter("@username", Data.SqlDbType.VarChar, 50))
                sql.Parameters("@username").Value = username

                sql.Parameters.Add(New SqlParameter("@profile", Data.SqlDbType.VarChar, 50))
                sql.Parameters("@profile").Value = profile
                oConn.Open()
                sql.ExecuteNonQuery()
                oConn.Close()


            End If

        Next row
        gvProfiles.DataBind()


    End Sub

Open in new window

<asp:GridView ID="gvProfiles" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" CssClass="gridview" DataSourceID="SqlDataProfiles"
        Width="607px" DataKeyNames="Username,Profile">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnselect" runat="server" CausesValidation="False" CommandName="Select"
                        Text="Select" OnClick="lbtnselect_Click" CommandArgument="Select"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Profile Name" SortExpression="Profile">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Profile") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblprofile" runat="server" Text='<%# Bind("Profile") %>'></asp:Label>
                </ItemTemplate>
                <ControlStyle CssClass="gridviewcontrol" />
                <HeaderStyle CssClass="gridviewheader" />
                <ItemStyle CssClass="gridviewitem" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Project Name" SortExpression="ProjectName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProjectName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblprojectname" runat="server" Text='<%# Bind("ProjectName") %>'></asp:Label>
                </ItemTemplate>
                <ControlStyle CssClass="gridviewcontrol" />
                <HeaderStyle CssClass="gridviewheader" />
                <ItemStyle CssClass="gridviewitem" />
            </asp:TemplateField>
            <asp:BoundField DataField="ModifiedDate" HeaderText="Modified" SortExpression="ModifiedDate">
                <ControlStyle CssClass="gridviewcontrol" />
                <HeaderStyle CssClass="gridviewheader" />
                <ItemStyle CssClass="gridviewitem" HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="CreateDate" HeaderText="Created" SortExpression="CreateDate">
                <ControlStyle CssClass="gridviewcontrol" />
                <HeaderStyle CssClass="gridviewheader" />
                <ItemStyle CssClass="gridviewitem" HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username"
                Visible="False" />
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:CheckBox ID="chkdelete" runat="server" />
                </ItemTemplate>
                <ControlStyle CssClass="gridviewcontrol" />
                <HeaderStyle CssClass="gridviewheader" />
                <ItemStyle CssClass="gridviewitem" HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
        <AlternatingRowStyle CssClass="gridviewalternating" />
        <EmptyDataTemplate>
            <strong>You have no saved Profiles. Use the Employee Selector to create a new profile.</strong>
        </EmptyDataTemplate>
    </asp:GridView>

Open in new window

0
Comment
Question by:imstac73
5 Comments
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33575263
What is happening when you are running this code?
0
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 33576076
Does it throw any error?
0
 

Author Comment

by:imstac73
ID: 33577447
Nothing happens.  I don't receive any errors but it doesn't delete the rows.  I did some breakpoints and it is not recognizing when the checkbox is checked.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33579940
You need to know the column index (0-based) of chkDelete and lblProfile. Try this:

            Dim bChecked As Boolean = CBool(row.Cells(chkDeleteColumnIndex).Text)
Dim profile As String = row.Cells(lblProfileColumnIndex).Text

            'Check if the checkbox is checked.


            If bChecked Then




0
 

Accepted Solution

by:
imstac73 earned 0 total points
ID: 33580757
I recreated my gridview but didn't change any of my backend code and it is now working again.  I must have gremlins.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question