insert data in table display in gridview with delete and edit button and checkbox for multiple delete by using three tier archicture
//.aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
name :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
lname :
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing1" OnRowUpdating="GridView1_RowUpdating" PageSize="5" AllowSorting="True" OnSorting="GridView1_Sorting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First
Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("fname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("fname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last
Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Delete" />
</div>
</form>
</body>
</html>
//.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
BAL b = new BAL();
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
public void bindgrid()
{
GridView1.DataSource
= b.selectdata();
GridView1.DataBind();
}
protected void
Button1_Click(object sender, EventArgs e)
{
b.fname =
TextBox2.Text;
b.lname =
TextBox3.Text;
b.insertstudent(b);
bindgrid();
}
protected void
GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
b.id = Convert.ToInt32(lblid.Text);
b.deletedata(b);
bindgrid();
}
protected void
Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count;
i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chk.Checked)
{
Label lblid = (Label)GridView1.Rows[i].FindControl("Label1");
b.id = Convert.ToInt32(lblid.Text);
b.deletedata(b);
}
}
bindgrid();
}
protected void
GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindgrid();
DropDownList ddlstate = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1");
ddlstate.DataSource
= b.selectstate();
ddlstate.DataTextField = "state";
ddlstate.DataValueField = "sid";
ddlstate.DataBind();
}
protected void
GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("Label4");
TextBox txtfname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
TextBox txtlname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
b.id = Convert.ToInt32(lblid.Text);
b.fname =
txtfname.Text;
b.lname =
txtlname.Text;
GridView1.EditIndex = -1;
b.updatedata(b);
bindgrid();
}
protected void
GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindgrid();
}
protected void
GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindgrid();
}
protected void
GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}
protected void
Button3_Click(object sender, EventArgs e)
{
Session["user"] = TextBox2.Text;
}
}
//DataAccessLayer file//.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary
description for BAL
/// </summary>
public class BAL:DAL
{
public BAL()
{
//
// TODO: Add constructor logic here
//
}
public int id { get; set; }
public string fname { get; set; }
public string lname { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
/// <summary>
/// Summary
description for DAL
/// </summary>
public class DAL
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["str"].ConnectionString);
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
public DAL()
{
//
// TODO: Add constructor logic here
//
}
public void insertstudent(BAL b)
{
cmd = new SqlCommand("insertstudents", con);
cmd.CommandType
= CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fnames", b.fname);
cmd.Parameters.AddWithValue("@lname", b.lname);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public DataSet selectdata()
{
cmd = new SqlCommand("selectstudents", con);
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
return ds;
}
public void deletedata(BAL b)
{
cmd = new SqlCommand("studentdelete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", b.id);
con_open();
cmd.ExecuteNonQuery();
con.Close();
}
public void updatedata(BAL b)
{
cmd = new SqlCommand("updatestudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", b.id);
cmd.Parameters.AddWithValue("@fname", b.fname);
cmd.Parameters.AddWithValue("@lname",b.lname);
con_open();
cmd.ExecuteNonQuery();
con.Close();
}
}