Solved

Gridview Delete multiple rows

Posted on 2010-08-31
5
600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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