Solved

Gridview Delete multiple rows

Posted on 2010-08-31
5
597 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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