code for html page
**************************************************
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="uid" HeaderText="ID"
SortExpression="ID"/>
<asp:TemplateField HeaderText="Name"
SortExpression="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server"
Text='<%# Bind("uname") %>' ReadOnly="true"
ForeColor="Blue" BorderStyle="none"
BorderWidth="0px" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location"
SortExpression="Location">
<ItemTemplate>
<asp:TextBox ID="txtLocation" runat="server"
Text='<%# Bind("city") %>'
ReadOnly="true" ForeColor="Blue"
BorderStyle="none" BorderWidth="0px">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1"
runat="server">
</asp:ToolkitScriptManager>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:stringname %>"
SelectCommand="SELECT [uid], [uname], [city] FROM [table_name]"
DeleteCommand="DELETE FROM table_name WHERE (uid = @ID)"
UpdateCommand="UPDATE [table_name] SET [uname] = @Name,
[city] = @Location WHERE [uid] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="Location" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:Label ID="lblmessage" runat="server" Text="Label"></
asp:Label>
<asp:Button ID="Button1" runat="server"
onclick="Button1_Click1"
Text="Button" />
<br />
<asp:Button ID="btnUpdate" runat="server"
OnClick="btnUpdate_Click" Text="Update" /><br />
<asp:Button ID="btnDelete" runat="server"
OnClick="btnDelete_Click"
OnClientClick="return DeleteConfirmation();"
Text="Delete" />
**************************************************************************
coding for aspx.cs page
+++++++++++++++++++++++++++++++++++
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//using System.Net.NetworkInformation;
//using System.Net.Security;
//using System.Net.Sockets;
//using System.Net.Mail;
using System.IO;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections.Specialized;
public partial class demo :
System.Web.UI.Page
{
string strConnection = ConfigurationManager.ConnectionStrings
["tennisConnectionString"].stringname;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
//Create stringbuilder to store multiple DML statements
StringBuilder strSql = new StringBuilder(string.Empty);
//Create sql connection and command
SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand();
//Loop through gridview rows to find checkbox
//and check whether it is checked or not
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
// Get the values of textboxes using findControl
string strID = GridView1.Rows[i].Cells[1].Text;
string strName = ((TextBox)
GridView1.Rows[i].FindControl("txtName")).Text;
string strLocation = ((TextBox)
GridView1.Rows[i].FindControl("txtLocation")).Text
try
{
string strUpdate = "Update table_name set uname = @uname,"
+" city = @city WHERE uid = @uid";
cmd.CommandType = CommandType.Text;
cmd.CommandText = strUpdate.ToString();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@uname", strName);
cmd.Parameters.AddWithValue("@city", strLocation);
cmd.Parameters.AddWithValue("@uid", strID);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string errorMsg = "Error in Updation";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
con.Close();
}
}
}
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//Create String Collection to store IDs of
//records to be deleted .
StringCollection idCollection = new StringCollection();
string strID = string.Empty;
//Loop through GridView rows to find checked rows
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].
Cells[0].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
idCollection.Add(strID);
}
}
}
if (idCollection.Count > 0)
{
//Call the method to Delete records
DeleteMultipleRecords(idCollection);
// rebind the GridView
GridView1.DataBind();
}
else
{
lblmessage.Text = "Please select any row to delete";
}
}
private void DeleteMultipleRecords(StringCollection idCollection)
{
//Create sql Connection and Sql Command
SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand();
string IDs = "";
foreach (string id in idCollection)
{
IDs += id.ToString() + ",";
}
try
{
string test = IDs.Substring
(0, IDs.LastIndexOf(","));
string sql = "Delete fromtable"_name +" WHERE uid in (" + test
+ ")";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string errorMsg = "Error in Deletion";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
con.Close();
}
}
//Write this code in the CheckedChanged Event of CheckBox
protected void chkSelect_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
grdRow.BackColor = Color.Red;
TextBox txtname = (TextBox)grdRow.FindControl
("txtName");
TextBox txtlocation = (TextBox)grdRow.FindControl
("txtLocation");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
txtlocation.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtlocation.ForeColor = System.Drawing.Color.Black;
}
else
{
grdRow.BackColor = Color.White;
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow rs in GridView1.Rows)
{
CheckBox chkUncheck = (CheckBox)
rs.FindControl("chkSelect");
if (chkUncheck.Checked)
{ chkUncheck.Checked = false; }
else
{ chkUncheck.Checked = true; }
}
}
}