Hi,
i am trying the code for deleting records in the gridview, but after selecting ckeckbox and clicking on delete button all the rows data is getting deleted, i wanted to delete only check box marked row data. i am not getting what mistake i have did. my sample code is shown below.
my .aspx code is
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Debug="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="cat_id" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
<asp:BoundField DataField="cat_id" HeaderText="Category ID" ReadOnly="True" />
<asp:BoundField DataField="cat_name" HeaderText="Category Name" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="Date" HeaderText="Date" />
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Add New Row"
onclick="Button1_Click" />
<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete Selected Records" />
</div>
</form>
</body>
</html>
and my .aspx.cs code is
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;
using System.Collections.Specialized;
using System.Text;
using System.Collections;
public partial class Default3 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string connectionString = "Data Source=hobvision07;Initial Catalog=master; user id=sa;password=hobvision";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
btnDelete.Attributes.Add("onclick",
"return confirm('Are you sure you want to delete selected item(s) ?');");
}
}
private void BindData()
{
SqlConnection con = new SqlConnection("Data source=hobvision07; initial catalog=master; user id=sa; password=hobvision");
SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name,location,date FROM quest_categories", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data source=hobvision07; initial catalog=master; user id=sa; password=hobvision");
SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name,location,date FROM quest_categories", con);
DataTable dt = new DataTable();
da.Fill(dt);
// Here we'll add a blank row to the returned DataTable
DataRow dr = dt.NewRow();
dt.Rows.InsertAt(dr, 0);
//Creating the first row of GridView to be Editable
GridView1.EditIndex = 0;
GridView1.DataSource = dt;
GridView1.DataBind();
//Changing the Text for Inserting a New Record
((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text = "Insert";
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text == "Insert")
{
SqlConnection con = new SqlConnection("Data source=hobvision07; initial catalog=master; user id=sa; password=hobvision");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO quest_categories(cat_name,location,date) VALUES(@cat_name,@location,@date)"; cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@location", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;
cmd.Parameters.Add("@date", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[5].Controls[0]).Text;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
else
{
SqlConnection con = new SqlConnection("Data source=hobvision07; initial catalog=master; user id=sa; password=hobvision");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE quest_categories SET cat_name=@cat_name,location=@location,date=@date WHERE cat_id=@cat_id";
cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@location", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
cmd.Parameters.Add("@date", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[2].Text);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
GridView1.EditIndex = -1;
BindData();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
ArrayList productsToDelete = new ArrayList();
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkDelete = (CheckBox)row.Cells[0].FindControl("chkDelete");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
string productId = row.Cells[1].Text;
productsToDelete.Add(productId);
}
}
}
}
DeleteProducts(productsToDelete);
BindData();
}
private void DeleteProducts(ArrayList productsToDelete)
{
foreach (GridViewRow row in GridView1.Rows)
{
string productId = row.Cells[2].Text;
SqlConnection con = new SqlConnection("Data source=hobvision07; initial catalog=master; user id=sa; password=hobvision");
SqlDataAdapter cmd = new SqlDataAdapter("delete from quest_categories where cat_id='" + productId + "'", con);
DataTable dt = new DataTable();
cmd.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
//BindData();
}
}
}
Please help me regarding this.