Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Source code:
 
  <Table border="3" align="Center">
        <asp:DataList ID="DataList1" runat="server">
            <HeaderTemplate>
                <tr>
                    <th><asp:CheckBox ID="ChkAll" runat="server" /></th>
                    <th>EmpId</th><th>EName</th><th>Designation</th><th>DOJ</th><th>Salary</th><th>Deptno</th>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <th><asp:CheckBox ID="ChkOne" runat="server" /></th>
                    <th><asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("EmpId") %>'></asp:Label></th>
                    <th><%#Eval("EName") %></th>
                    <th><%#Eval("Designation") %></th>
                    <th><%#Eval("DOJ") %></th>
                    <th><%#Eval("Salary") %></th>
                    <th><%#Eval("Deptno") %></th>
                </tr>
            </ItemTemplate>
            
        </asp:DataList>
    </Table>
 
C# Code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
public partial class DeleteMultipleRecordsUsingDataListControl : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=Dexter;User Id=sa;Password=123;Database=Vishnu");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
            GetData();
    }
    private void GetData()
    {
        SqlDataAdapter Da = new SqlDataAdapter("select*from EmpDetails", con);
        DataSet Ds = new DataSet();
        Da.Fill(Ds, "X");
        DataList1.DataSource = Ds.Tables[0];
        DataList1.DataBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        con.Open();
        foreach(DataListItem L in DataList1.Items)
        {
            CheckBox C1 = (CheckBox)L.FindControl("ChkOne");
            if (C1.Checked == true)
            {
                Label L1 = (Label)L.FindControl("lblEmpId");
                string s = "Delete EmpDetails Where EmpId=" + L1.Text;
                SqlCommand cmd = new SqlCommand(s, con);
                cmd.ExecuteNonQuery();
            }
            con.Close();
            GetData();
        }
    }
}
 

 
And I am Getting Runtime Error Like this [Collection was modified; enumeration operation may not execute.]
Posted 27-Feb-13 7:42am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should not delete the the items from the collection you are iterating.
 
foreach(DataListItem L in DataList1.Items)
       {
 
Instead you should first create the list of checked items .
Then delete them from database.
Then fire the select query and then bind .
 
This way a select query will also be fired only once , even for multiple deletes.
  Permalink  
v2
Comments
Dwijender at 27-Feb-13 13:17pm
   
i am selecting the record at the execution time then why would i write instead of this foreach loop???
Dwijender at 27-Feb-13 13:22pm
   
Could you post me the Code for Delete button ???
aspnet_regiis -i at 28-Feb-13 10:39am
   
You write the code according to the steps I have mentioned and if anything goes wrong, I will correct it...Your company pays you for writing the code..Not me
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

basically you can't modify an enumeration (for each) like adding or deleting items while you are looping, that is because if you are in the middle of the loop and some items got removed when ever you get to the next item it might already been removed, you need to modify your logic here and probably use a FOR loop instead of a FOREACH: something like this:
 

 
for (int i = list.Count - 1; i >= 0; i--)
{
    if (true)
    	list.RemoveAt(i);
}
 

and I believe that Lists do support Reverse traversing (not sure) but the key is to do it backwards as shown using the for loop
  Permalink  
v2
Comments
Dwijender at 27-Feb-13 13:20pm
   
I don't Understand you??? and i think this code is not correct....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use following code...
protected void btnDelete_Click(object sender, EventArgs e)
    {
        List<string> tempEmpID = new List<string>();
        con.Open();
        foreach(DataListItem L in DataList1.Items)
        {
            CheckBox C1 = (CheckBox)L.FindControl("ChkOne");
            if (C1.Checked == true)
            {
                Label L1 = (Label)L.FindControl("lblEmpId");
                tempEmpID.add(L1.Text);
            }
                      
        }
        Foreach(string str in tempEmpID)
        {
           if(con.ConnectionState == ConnectionState.Close)
               con.Open();
           string s = "Delete EmpDetails Where EmpId=" + L1.Text;
           SqlCommand cmd = new SqlCommand(s, con);
           cmd.ExecuteNonQuery();
                      
        } 
        con.Close();
        GetData();
    }</string></string>
  Permalink  
Comments
Dwijender at 28-Feb-13 2:52am
   
I am getting so many errors by this code u execute this???
 
IN this
 
Foreach(string str in tempEmpID)
{
 
how did we use L1.text???
 
and What is this
 
<pre lang="c#">
List tempEmpID = new List();
</pre>
 
i don't understand this???
 
can u explain me??
 
Thanks ........
vishal.shimpi at 1-Mar-13 0:11am
   
sorry edit your query like...
string s = "Delete EmpDetails Where EmpId=" + str;
and declare tempEmpID like List<string> tempEmpID = new List<string>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 240
2 DamithSL 200
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100