Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,
plz can u suggest me C# code...for delete using checkbox in asp.net using c#..
this is my design page...
--------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
XML
<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
Updated 2-Sep-12 21:02pm
v2
Comments
Devang Vaja 3-Sep-12 2:27am    
what you want to delete actually?????

value from database how r they stored????please elaborate...
Member 9376025 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">
<itemtemplate>
<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 3-Sep-12 3:13am    
I am unable to get your code?

Try this:

This is how you can get datasource:
XML
<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:
XML
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkRows" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>


This will add checkBox in Grid:
XML
<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:
C#
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[^]
 
Share this answer
 
v2
Comments
_Amy 3-Sep-12 2:31am    
My +5! :)
Prasad_Kulkarni 3-Sep-12 2:32am    
Thank you Amit!
Mohamed Mitwalli 3-Sep-12 2:34am    
5+
Prasad_Kulkarni 3-Sep-12 2:43am    
Thank you Mohamed!
Member 9376025 3-Sep-12 2:35am    
not using gridview
Hi ,
Check this example
C#
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
 
Share this answer
 
v2
Comments
_Amy 3-Sep-12 2:35am    
5'ed!
Mohamed Mitwalli 3-Sep-12 2:39am    
Thanks Amy :)
Prasad_Kulkarni 3-Sep-12 2:45am    
My 5!
Mohamed Mitwalli 3-Sep-12 2:54am    
Thanks Prasad :)
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:

C#
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.
 
Share this answer
 
v2

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