ASP.net ile checkboxdan seçilen kayıtları databaseden silme

Default.aspx
      
        <asp:DataList ID="DataList1" runat="server">
        <ItemTemplate>
          <asp:CheckBox ID="c" runat="server" />
          <asp:Label ID="l" runat="server" Text='<%# Eval("Mail") %>'></asp:Label>
          <asp:Label runat="server" Visible="false" ID="lblid" Text='<%# Eval("ID") %>'></asp:Label>
        </ItemTemplate>
        </asp:DataList>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Seçilenleri Sil" />
 
Şimdi de kodları aşağıdaki gibi yazalım
(sayfamıza using System.Data.OleDb ve using System.Data name spacelerini  eklemyi untmuyoruz tabi)

Default.aspx.cs

   OleDbConnection bg = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=" + HttpContext.Current.Server.MapPath("~/App_Data/Kobay.mdb"));
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            VeriGetir();
        }
    }
 
    private void VeriGetir()
    {
        bg.Open();
        OleDbCommand cmd = new OleDbCommand("Select * FROM Mail", bg);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataTable dt = new DataTable("tbl");
        da.Fill(dt);
        DataList1.DataSource = dt.DefaultView;
        DataList1.DataBind();
        bg.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList dizi=new ArrayList();
 
        foreach (DataListItem item in DataList1.Items)
        {
            CheckBox c = (CheckBox)item.FindControl("c");
            if (c.Checked)
            {
                Label lbl = (Label)item.FindControl("lblid");
                dizi.Add(lbl.Text);
            }
        }
 
        for (int i = 0; i < dizi.Count; i++)
        {
                KayitSil(dizi[i].ToString());
        }
        VeriGetir();
 
         
    }
 
    private void KayitSil(string p)
    {
       int id = Int32.Parse(p);
 
       bg.Open();
       OleDbCommand cmd = new OleDbCommand("delete FROM Mail Where ID=" + id + " ", bg);
       cmd.ExecuteNonQuery();
       bg.Close();
    }
 
Alıntı