Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Gridview Delete multiple rows

Posted on 2010-08-31
5
Medium Priority
?
603 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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