Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#3.5
Dear Experts,
plz can u suggest me C# code...for delete using checkbox in asp.net using c#..
this is my design page...
--------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
<tr>
    <td>
        <asp:CheckBox ID="CheckBox2" runat="server" Text="Family" Width="170px"  />
 
    </td>
    </tr>
    <tr>
    <td>
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Friends"  Width="170px"
            Height="21px"/>
    </td>
    </tr>
    <tr>
    <td>
        <asp:CheckBox ID="CheckBox3" runat="server" Text="Others"  Width="170px"/>
    </td>
    </tr>
 
    <tr>
    <td>
        <asp:Button ID="btnDelete"  runat="server" Text="Delete"
            onclick="btnDelete_Click" />
    </td>
    </tr>
 
this is all my pages..what i have used as with database admin page: As source page ie.,aspx Select Type : <asp:DropDownList ID="ddltype" AppendDataBoundItems="true" runat="server" SkinID="DDListNormalRequired" Width="170px"> <asp:ListItem Value="-1">Select <asp:ListItem >Family <asp:ListItem>Friends <asp:ListItem>others Enter Name : <asp:TextBox ID="txtName" runat="server" Width="170px"> SelectImage : <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnInsert" runat="server" Text="Insert" Width="100" onclick="btnInsert_Click"/> now aspx.cs page SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sss"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { } protected void btnInsert_Click(object sender, EventArgs e) { try { string s = FileUpload1.PostedFile.FileName; string a = Server.MapPath(".")+"//images//"; FileUpload1.SaveAs(a + s); string imagepath = "Admin/images/" + s; SqlCommand cmd = new SqlCommand("type", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@selecttype", ddltype.Text); cmd.Parameters.AddWithValue("@entername ", txtName.Text); cmd.Parameters.Add("@imagepath ", imagepath); con.Open(); cmd.ExecuteNonQuery(); con.Close(); //Response.Redirect("~Default2.aspx"); } catch(Exception ex) { throw ex; } second page
<asp:DataList ID="DataList1" runat="server"> <asp:HyperLink ID="family" runat="server" Navigateurl='<%#"Default2.aspx?Id="+DataBinder.Eval(Container.DataItem,"id").ToString()+""%>' Text='<%#Eval("names") %>'>
aspx.cs page protected void Page_Load(object sender, EventArgs e) { fillgroups(); } private void fillgroups() { string s = "select * from groups "; SqlDataAdapter da = new SqlDataAdapter(s, con); DataSet ds = new DataSet(); da.Fill(ds,"groups"); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } thrid page
<asp:CheckBox ID="CheckBox2" runat="server" Text="Family" Width="170px" />
<asp:CheckBox ID="CheckBox1" runat="server" Text="Friends" Width="170px" Height="21px"/> <asp:CheckBox ID="CheckBox3" runat="server" Text="Others" Width="170px"/> <asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" /> for thrid page i want C# code the database table is Family varchar(50) Checked Friends varchar(50) Checked Others varchar(50) Checked id int Checked name varbinary(50) Checked names varchar(MAX) Checked Selecttype varchar(50) Checked SelectName varchar(50) Checked SelectImage image Checked imagepath varchar(MAX) Checked Stored procedure code ALTER procedure [dbo].[details] @Family varchar(50), @Friends varchar(50), @Others varchar(50) as
Posted 2-Sep-12 20:21pm
Edited 2-Sep-12 21:02pm
v2
Comments
Devang Vaja at 3-Sep-12 2:27am
   
what you want to delete actually?????
 
value from database how r they stored????please elaborate...
Member 9376025 at 3-Sep-12 2:43am
   
this is all my pages..what i have used as with database
admin page:
As source page ie.,aspx
<tr>
<td>
Select Type
</td>
<td>
:
</td>
<td width="150">
<asp:DropDownList ID="ddltype" AppendDataBoundItems="true" runat="server" SkinID="DDListNormalRequired" Width="170px">
<asp:ListItem Value="-1">Select
<asp:ListItem >Family
<asp:ListItem>Friends
<asp:ListItem>others

</td>
</tr>
<tr>
<td>Enter Name</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Width="170px">
</td>
</tr>
<tr>
<td>SelectImage</td>
<td>
:
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
 
</tr>
<tr>
<td>
<asp:Button ID="btnInsert" runat="server" Text="Insert" Width="100"
onclick="btnInsert_Click"/>
</td>
</tr>
now aspx.cs page
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sss"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
 

}
protected void btnInsert_Click(object sender, EventArgs e)
{
try
{

string s = FileUpload1.PostedFile.FileName;
string a = Server.MapPath(".")+"//images//";
FileUpload1.SaveAs(a + s);
string imagepath = "Admin/images/" + s;
 
SqlCommand cmd = new SqlCommand("type", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@selecttype", ddltype.Text);
cmd.Parameters.AddWithValue("@entername ", txtName.Text);
 
cmd.Parameters.Add("@imagepath ", imagepath);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
//Response.Redirect("~Default2.aspx");

}
catch(Exception ex)
{
throw ex;

}
second page
<div>
<asp:DataList ID="DataList1" runat="server">

<asp:HyperLink ID="family" runat="server" Navigateurl='<%#"Default2.aspx?Id="+DataBinder.Eval(Container.DataItem,"id").ToString()+""%>' Text='<%#Eval("names") %>'>


</div>
aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{


fillgroups();

 

 

}
private void fillgroups()
{
string s = "select * from groups ";
SqlDataAdapter da = new SqlDataAdapter(s, con);
DataSet ds = new DataSet();
da.Fill(ds,"groups");
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
thrid page
<td>
<asp:CheckBox ID="CheckBox2" runat="server" Text="Family" Width="170px" />

</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text="Friends" Width="170px"
Height="21px"/>
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="CheckBox3" runat="server" Text="Others" Width="170px"/>
</td>
</tr>

<tr>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete"
onclick="btnDelete_Click" />
</td>
</tr>
for thrid page i want C# code
 
the database table is
Family varchar(50) Checked
Friends varchar(50) Checked
Others varchar(50) Checked

id int Checked
name varbinary(50) Checked
names varchar(MAX) Checked

Selecttype varchar(50) Checked
SelectName varchar(50) Checked
SelectImage image Checked
imagepath varchar(MAX) Checked
 
Stored procedure code
ALTER procedure [dbo].[details]
@Family varchar(50),
@Friends varchar(50),
@Others varchar(50)
as
Prasad_Kulkarni at 3-Sep-12 3:13am
   
I am unable to get your code?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
 
This is how you can get datasource:
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
    SelectCommand="SELECT EmployeeID, LastName, City FROM Employees"
    DeleteCommand="DELETE FROM Employees WHERE [EmployeeID] = @EmployeeID"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" >
 
    <DeleteParameters>
        <asp:Parameter Name="EmployeeID" />
    </DeleteParameters>
</asp:SqlDataSource>
 
This is template field for CheckBox in Grid:
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkRows" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>
 
This will add checkBox in Grid:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="cbRows" runat="server"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
    </Columns>
</asp:GridView>
 
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
    SelectCommand="SELECT EmployeeID, LastName, City FROM Employees"
    DeleteCommand="DELETE FROM Employees WHERE [EmployeeID] = @EmployeeID"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" >
 
    <DeleteParameters>
        <asp:Parameter Name="EmployeeID" />
    </DeleteParameters>
</asp:SqlDataSource>
 
<asp:Button ID="btnMultipleRowDelete"  OnClick="btnMultipleRowDelete_Click" runat="server"  Text="Delete Rows" />
 
Code for deletion of multiple records from Grid:
protected void btnMultipleRowDelete_Click(object sender, EventArgs e)
{
    // Looping through all the rows in the GridView
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox checkbox = (CheckBox)row.FindControl("cbRows");
 
        //Check if the checkbox is checked.
        //value in the HtmlInputCheckBox's Value property is set as the

        //value of the delete command's parameter.
        if (checkbox.Checked)
        {
            // Retreive the Employee ID
            int employeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
 
            // Pass the value of the selected Employye ID to the Delete //command.
            SqlDataSource1.DeleteParameters["EmployeeID"].DefaultValue = employeeID.ToString();
            SqlDataSource1.Delete();
        }
    }
}
Hope it helps.
 
Also refer some related links:
Delete Multiple Rows Records Gridview CheckBox Confirmation[^]
Add, Edit, and Delete in DataGridView with Paging[^]
 
Similar discussion:
delete all selected rows in DataGridView [^]
How to delete multiple rows in a DataGridView[^]
delete multiple rows in datagridview[^]
 
This one exact matching your requirement:
delete multiple rows in datagridview and access table[^]
[EDIT]
Refer for deleting records from DataList:
Delete multiple records from repeater or Datalist[^]
  Permalink  
v2
Comments
_Amy at 3-Sep-12 2:31am
   
My +5! :)
Prasad_Kulkarni at 3-Sep-12 2:32am
   
Thank you Amit!
Mohamed Mitwalli at 3-Sep-12 2:34am
   
5+
Prasad_Kulkarni at 3-Sep-12 2:43am
   
Thank you Mohamed!
Member 9376025 at 3-Sep-12 2:35am
   
not using gridview
Member 9376025 at 3-Sep-12 2:35am
   
i want with out gridview
Prasad_Kulkarni at 3-Sep-12 2:43am
   
How you are showing multiple records then??
Member 9376025 at 3-Sep-12 2:52am
   
can i post here my code...
can u help
Prasad_Kulkarni at 3-Sep-12 2:56am
   
Yes of course.
But post your code in your question itself by using 'Improve question' widget.
Member 9376025 at 3-Sep-12 3:03am
   
this is all my pages..what i have used as with database admin page: As source page ie.,aspx <tr> <td> Select Type </td> <td> : </td> <td width="150"> <asp:DropDownList ID="ddltype" AppendDataBoundItems="true" runat="server" SkinID="DDListNormalRequired" Width="170px"> <asp:ListItem Value="-1">Select <asp:ListItem >Family <asp:ListItem>Friends <asp:ListItem>others </td> </tr> <tr> <td>Enter Name</td> <td> : </td> <td> <asp:TextBox ID="txtName" runat="server" Width="170px"> </td> </tr> <tr> <td>SelectImage</td> <td> : </td> <td> <asp:FileUpload ID="FileUpload1" runat="server" /> </td> </tr> <tr> <td> <asp:Button ID="btnInsert" runat="server" Text="Insert" Width="100" onclick="btnInsert_Click"/> </td> </tr> now aspx.cs page SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sss"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { } protected void btnInsert_Click(object sender, EventArgs e) { try { string s = FileUpload1.PostedFile.FileName; string a = Server.MapPath(".")+"//images//"; FileUpload1.SaveAs(a + s); string imagepath = "Admin/images/" + s; SqlCommand cmd = new SqlCommand("type", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@selecttype", ddltype.Text); cmd.Parameters.AddWithValue("@entername ", txtName.Text); cmd.Parameters.Add("@imagepath ", imagepath); con.Open(); cmd.ExecuteNonQuery(); con.Close(); //Response.Redirect("~Default2.aspx"); } catch(Exception ex) { throw ex; } second page <div> <asp:DataList ID="DataList1" runat="server"> <asp:HyperLink ID="family" runat="server" Navigateurl='<%#"Default2.aspx?Id="+DataBinder.Eval(Container.DataItem,"id").ToString()+""%>' Text='<%#Eval("names") %>'> </div> aspx.cs page protected void Page_Load(object sender, EventArgs e) { fillgroups(); } private void fillgroups() { string s = "select * from groups "; SqlDataAdapter da = new SqlDataAdapter(s, con); DataSet ds = new DataSet(); da.Fill(ds,"groups"); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } thrid page <td> <asp:CheckBox ID="CheckBox2" runat="server" Text="Family" Width="170px" /> </td> </tr> <tr> <td> <asp:CheckBox ID="CheckBox1" runat="server" Text="Friends" Width="170px" Height="21px"/> </td> </tr> <tr> <td> <asp:CheckBox ID="CheckBox3" runat="server" Text="Others" Width="170px"/> </td> </tr> <tr> <td> <asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" /> </td> </tr> for thrid page i want C# code the database table is Family varchar(50) Checked Friends varchar(50) Checked Others varchar(50) Checked id int Checked name varbinary(50) Checked names varchar(MAX) Checked Selecttype varchar(50) Checked SelectName varchar(50) Checked SelectImage image Checked imagepath varchar(MAX) Checked Stored procedure code ALTER procedure [dbo].[details] @Family varchar(50), @Friends varchar(50), @Others varchar(50) as
Prasad_Kulkarni at 3-Sep-12 3:14am
   
I am not able to get this.
Prasad_Kulkarni at 3-Sep-12 3:30am
   
Refer new link added in my answer.
Manas Bhardwaj at 3-Sep-12 6:07am
   
nice +5!
Prasad_Kulkarni at 3-Sep-12 6:12am
   
Thank you Manas!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi ,
Check this example
protected void Button2_Click(object sender, EventArgs e)
   {
       if (chk.Checked == true)
       {
           DeleteRecored(Convert.ToInt32(txtID.Text));
       }
       else if (chk2.Checked == true)
       {
           DeleteRecored(Convert.ToInt32(txtID.Text));
       }
  }
   void DeleteRecored(int id)
 {
 
     using (SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=***"))
     {
         con.Open();
         using (SqlCommand cmd = new SqlCommand("delete   from Product where id =@id ",con))
         {
             cmd.Parameters.AddWithValue("@id", id);
             cmd.ExecuteNonQuery();
 
         }
 
     }
 }
 
Best Regards
M.Mitwalli
  Permalink  
v2
Comments
_Amy at 3-Sep-12 2:35am
   
5'ed!
Mohamed Mitwalli at 3-Sep-12 2:39am
   
Thanks Amy :)
Prasad_Kulkarni at 3-Sep-12 2:45am
   
My 5!
Mohamed Mitwalli at 3-Sep-12 2:54am
   
Thanks Prasad :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Add Check Box Column to asp.net data grid.
Convert it to template field.
Go to Edit Templates in YourGridView's smart task pane.
Select Item template.
Select the check box.
Go to properties and set autopostback to true.
Double click on check box.it will take you to code part.
 
In CheckBox1_CheckedChanged event and write this code:
 
foreach(gridviewrow r in [YourGridViewName].Rows)
{
if(((CheckBox)r.FindControll("CheckBox1")).Checked)
{
//Calls the delete method passing delete criteria as a parameter
DeleteRow(r.Cells[index of Delete Criteria Column].Text);
}
}
 
I hope Deleting using Sql query you know.
  Permalink  
v2

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

  Print Answers RSS
0 CPallini 325
1 Sergey Alexandrovich Kryukov 249
2 OriginalGriff 230
3 George Jonsson 130
4 ChauhanAjay 104
0 OriginalGriff 6,464
1 Sergey Alexandrovich Kryukov 5,915
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 5 Sep 2012
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