Solved

Gridview Delete multiple rows

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now