Today In this example we will do a CRUD operation using without database i.e we will perform CRUD operation using a data table. Let see step by step.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<h1 style="color:green">Welcome To CURD Operation Without Database</h1>
<div style="width: 55%; background-image: url(Images/mks1.jpg); border: 5px solid yellow; border-radius: 25px; box-shadow: maroon 10px 10px 10px; color: aliceblue">
<table>
<tr>
<td></td>
<th>Emp ID:
</th>
<th>Emp Name:
</th>
<th>Dept Name :
</th>
<th>Emp Address :
</th>
<th>Emp Salary:
</th>
</tr>
<tr>
<td></td>
<td>
<asp:TextBox ID="txtEmpId" runat="server" Width="120px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtEmpName" runat="server" Width="120px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtDeptName" runat="server" Width="120px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtEmpAddress" runat="server" Width="120px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtEmpSalary" runat="server" Width="120px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="6" align="right">
<asp:Button ID="AddEmpDetails" runat="server" Style="color: White" Text="Add Employee"
OnClick="AddEmpDetails_Click" BackColor="#00248E" />
</td>
</tr>
</table>
<div style="margin-left: 10px; margin-top: 10px">
<asp:GridView ID="GridView1" Width="700" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" AutoGenerateColumns="False" runat="server" CellPadding="4" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" HeaderText="Operation" ItemStyle-Width="120px" />
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Id" DataField="EmpId">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Name" DataField="EmpName">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Dept Name" DataField="DeptName">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Address" DataField="EmpAddress">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Salary" DataField="EmpSalary">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
</div>
<br />
</div>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DefaultEmpRecord();
}
}
private void DefaultEmpRecord()
{
//creating dataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "EmployeeDetails";
dt.Columns.Add(new DataColumn("EmpId", typeof(string)));
dt.Columns.Add(new DataColumn("EmpName", typeof(string)));
dt.Columns.Add(new DataColumn("DeptName", typeof(string)));
dt.Columns.Add(new DataColumn("EmpAddress", typeof(string)));
dt.Columns.Add(new DataColumn("EmpSalary", typeof(string)));
dr = dt.NewRow();
dt.Rows.Add(dr);
//saving databale into viewstate
ViewState["EmployeeDetails"] = dt;
//bind Gridview
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["EmployeeDetails"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["EmployeeDetails"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["EmpId"] = txtEmpId.Text;
drCurrentRow["EmpName"] = txtEmpName.Text;
drCurrentRow["DeptName"] = txtDeptName.Text;
drCurrentRow["EmpAddress"] = txtEmpAddress.Text;
drCurrentRow["EmpSalary"] = txtEmpSalary.Text;
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
protected void AddEmpDetails_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(string)));
dt.Columns.Add(new DataColumn("EmpName", typeof(string)));
dt.Columns.Add(new DataColumn("DeptName", typeof(string)));
dt.Columns.Add(new DataColumn("EmpAddress", typeof(string)));
dt.Columns.Add(new DataColumn("EmpSalary", typeof(string)));
string Id = string.Empty;
string Name = string.Empty;
string DeptName = string.Empty;
string EmpAddress = string.Empty;
string EmpSalary = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = (GridViewRow)GridView1.Rows[i];
if (i != e.RowIndex)
{
Id = row.Cells[1].Text;
Name = row.Cells[2].Text;
DeptName = row.Cells[3].Text;
EmpAddress = row.Cells[4].Text;
EmpSalary = row.Cells[5].Text;
}
else
{
Id = ((TextBox)row.Cells[1].Controls[0]).Text;
Name = ((TextBox)row.Cells[2].Controls[0]).Text;
DeptName = ((TextBox)row.Cells[3].Controls[0]).Text;
EmpAddress = ((TextBox)row.Cells[4].Controls[0]).Text;
EmpSalary = ((TextBox)row.Cells[5].Controls[0]).Text;
}
DataRow dr = dt.NewRow();
dr["EmpId"] = Id;
dr["EmpName"] = Name;
dr["DeptName"] = DeptName;
dr["EmpAddress"] = EmpAddress;
dr["EmpSalary"] = EmpSalary;
dt.Rows.Add(dr);
}
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(string)));
dt.Columns.Add(new DataColumn("EmpName", typeof(string)));
dt.Columns.Add(new DataColumn("DeptName", typeof(string)));
dt.Columns.Add(new DataColumn("EmpAddress", typeof(string)));
dt.Columns.Add(new DataColumn("EmpSalary", typeof(string)));
string Id = string.Empty;
string Name = string.Empty;
string DeptName = string.Empty;
string EmpAddress = string.Empty;
string EmpSalary = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = (GridViewRow)GridView1.Rows[i];
if (i != e.RowIndex)
{
Id = row.Cells[1].Text;
Name = row.Cells[2].Text;
DeptName = row.Cells[3].Text;
EmpAddress = row.Cells[4].Text;
EmpSalary = row.Cells[5].Text;
DataRow dr = dt.NewRow();
dr["EmpId"] = Id;
dr["EmpName"] = Name;
dr["DeptName"] = DeptName;
dr["EmpAddress"] = EmpAddress;
dr["EmpSalary"] = EmpSalary;
dt.Rows.Add(dr);
}
}
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindEmpDetails();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindEmpDetails();
}
private void BindEmpDetails()
{
if (ViewState["EmployeeDetails"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["EmployeeDetails"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["EmpId"] = txtEmpId.Text;
drCurrentRow["EmpName"] = txtDeptName.Text;
drCurrentRow["DeptName"] = txtDeptName.Text;
drCurrentRow["EmpAddress"] = txtEmpAddress.Text;
drCurrentRow["EmpSalary"] = txtEmpSalary.Text;
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
}
Output:
->Fill all Text-boxes and Click Add Employee Button the see the result
->After clicking the Edit button
After click delete button
No comments:
Post a Comment