Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Source code:

ASP.NET
<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:

C#
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

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>
 
Share this answer
 
Comments
Dwijender 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<string> tempEmpID = new List<string>();
</pre>

i don't understand this???

can u explain me??

Thanks ........
vishal.shimpi 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>();
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:



C#
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
 
Share this answer
 
v2
Comments
Dwijender 27-Feb-13 13:20pm    
I don't Understand you??? and i think this code is not correct....
You should not delete the the items from the collection you are iterating.

C#
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.
 
Share this answer
 
v2
Comments
Dwijender 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 27-Feb-13 13:22pm    
Could you post me the Code for Delete button ???
bbirajdar 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900