Edit and delete in grid view code:
Login.aspx.cs :
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.Collections;
using Factory;
using BAL;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
string str = null;
public static string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["Sqlconnection"].ToString();
SqlConnection con = new SqlConnection(sqlc);
EmpBO bo = new EmpBO();
EmpBal bl = new EmpBal();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
display();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
bo.Firstname = TextBox1.Text;
bo.Lastname = TextBox2.Text;
bo.Empid = Convert.ToInt32(TextBox3.Text);
bo.Dept = DropDownList1.SelectedItem.Text;
bo.Salary = Convert.ToInt32(TextBox5.Text);
bo.Gender = RadioButtonList1.SelectedItem.Text;
for (int n = 0; n < CheckBoxList1.Items.Count; n++)
{
if (CheckBoxList1.Items[n].Selected)
{
if (str == null)
str += CheckBoxList1.Items[n].Text;
else
str += ", " + CheckBoxList1.Items[n].Text;
}
}
bo.Hobbies = str;
ds = bl.passto(bo);
display();
CleartextBoxes(this);
DropDownList1.SelectedIndex = 0;
}
catch (Exception)
{
throw;
}
}
public void display()
{
try
{
SqlConnection con = new SqlConnection(sqlc);
SqlCommand cmd = new SqlCommand("Select * from emp", con);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sd.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception)
{
throw;
}
}
public void CleartextBoxes(Control parent)
{
foreach (Control x in parent.Controls)
{
if ((x.GetType() == typeof(TextBox)))
{
((TextBox)(x)).Text = "";
}
if (x.HasControls())
{
CleartextBoxes(x);
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
TextBox texte = (TextBox)row.Cells[0].Controls[0];
TextBox textf = (TextBox)row.Cells[1].Controls[0];
TextBox textl = (TextBox)row.Cells[2].Controls[0];
TextBox textd = (TextBox)row.Cells[3].Controls[0];
TextBox texts = (TextBox)row.Cells[4].Controls[0];
TextBox textg = (TextBox)row.Cells[5].Controls[0];
TextBox texth = (TextBox)row.Cells[6].Controls[0];
GridView1.EditIndex = -1;
con.Open();
SqlCommand cmd = new SqlCommand("update emp set Firstname='" + textf.Text + "',Lastname='" + textl.Text + "',Dept='" + textd.Text + "' ,Salary=" + texts.Text + ",Gender='" + textg.Text + "' ,Hobbies='" + texth.Text + "' where Empid=" + userid + "", con);
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
display();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
display();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
display();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM emp where Empid='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
display();
}
}
Design:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Firstname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Lastname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Empid"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" MaxLength="3"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Dept"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>select</asp:ListItem>
<asp:ListItem>CSE</asp:ListItem>
<asp:ListItem>ECE</asp:ListItem>
<asp:ListItem>MECH</asp:ListItem>
<asp:ListItem>Testing</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Salary"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="Gender"></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="Hobbies"></asp:Label>
</td>
<td>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>PlayingCricket</asp:ListItem>
<asp:ListItem>Reading</asp:ListItem>
<asp:ListItem>Dancing</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4" DataKeyNames="Empid"
AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BorderStyle="None">
<Columns>
<asp:BoundField DataField="Empid" HeaderText="Empid" />
<asp:BoundField DataField="Firstname" HeaderText="Firstname." />
<asp:BoundField DataField="Lastname" HeaderText="Lastname" />
<asp:BoundField DataField="Dept" HeaderText="Dept" />
<asp:BoundField DataField="Salary" HeaderText="Salary" />
<asp:BoundField DataField="Gender" HeaderText="Gender" />
<asp:BoundField DataField="Hobbies" HeaderText="Hobbies" />
<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</form>
</body>
</html>
Login.aspx.cs :
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.Collections;
using Factory;
using BAL;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
string str = null;
public static string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["Sqlconnection"].ToString();
SqlConnection con = new SqlConnection(sqlc);
EmpBO bo = new EmpBO();
EmpBal bl = new EmpBal();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
display();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
bo.Firstname = TextBox1.Text;
bo.Lastname = TextBox2.Text;
bo.Empid = Convert.ToInt32(TextBox3.Text);
bo.Dept = DropDownList1.SelectedItem.Text;
bo.Salary = Convert.ToInt32(TextBox5.Text);
bo.Gender = RadioButtonList1.SelectedItem.Text;
for (int n = 0; n < CheckBoxList1.Items.Count; n++)
{
if (CheckBoxList1.Items[n].Selected)
{
if (str == null)
str += CheckBoxList1.Items[n].Text;
else
str += ", " + CheckBoxList1.Items[n].Text;
}
}
bo.Hobbies = str;
ds = bl.passto(bo);
display();
CleartextBoxes(this);
DropDownList1.SelectedIndex = 0;
}
catch (Exception)
{
throw;
}
}
public void display()
{
try
{
SqlConnection con = new SqlConnection(sqlc);
SqlCommand cmd = new SqlCommand("Select * from emp", con);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sd.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception)
{
throw;
}
}
public void CleartextBoxes(Control parent)
{
foreach (Control x in parent.Controls)
{
if ((x.GetType() == typeof(TextBox)))
{
((TextBox)(x)).Text = "";
}
if (x.HasControls())
{
CleartextBoxes(x);
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
TextBox texte = (TextBox)row.Cells[0].Controls[0];
TextBox textf = (TextBox)row.Cells[1].Controls[0];
TextBox textl = (TextBox)row.Cells[2].Controls[0];
TextBox textd = (TextBox)row.Cells[3].Controls[0];
TextBox texts = (TextBox)row.Cells[4].Controls[0];
TextBox textg = (TextBox)row.Cells[5].Controls[0];
TextBox texth = (TextBox)row.Cells[6].Controls[0];
GridView1.EditIndex = -1;
con.Open();
SqlCommand cmd = new SqlCommand("update emp set Firstname='" + textf.Text + "',Lastname='" + textl.Text + "',Dept='" + textd.Text + "' ,Salary=" + texts.Text + ",Gender='" + textg.Text + "' ,Hobbies='" + texth.Text + "' where Empid=" + userid + "", con);
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
display();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
display();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
display();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM emp where Empid='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
display();
}
}
Design:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Firstname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Lastname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Empid"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" MaxLength="3"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Dept"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>select</asp:ListItem>
<asp:ListItem>CSE</asp:ListItem>
<asp:ListItem>ECE</asp:ListItem>
<asp:ListItem>MECH</asp:ListItem>
<asp:ListItem>Testing</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Salary"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="Gender"></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="Hobbies"></asp:Label>
</td>
<td>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>PlayingCricket</asp:ListItem>
<asp:ListItem>Reading</asp:ListItem>
<asp:ListItem>Dancing</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4" DataKeyNames="Empid"
AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BorderStyle="None">
<Columns>
<asp:BoundField DataField="Empid" HeaderText="Empid" />
<asp:BoundField DataField="Firstname" HeaderText="Firstname." />
<asp:BoundField DataField="Lastname" HeaderText="Lastname" />
<asp:BoundField DataField="Dept" HeaderText="Dept" />
<asp:BoundField DataField="Salary" HeaderText="Salary" />
<asp:BoundField DataField="Gender" HeaderText="Gender" />
<asp:BoundField DataField="Hobbies" HeaderText="Hobbies" />
<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</form>
</body>
</html>
No comments:
Post a Comment