CRUD Operation Without Database In ASP.NET

I am going to explain about how to perform CRUD operations without using a database; i.e., I have neither used any database of SQL Server nor used any connection string. I took here an example that creates a bill of a grocery shop. Follow along step by step

Step 1

  • We have to create a Webpage, for which we open the Visual Studio -> new option and select the new Website.

  • Chose an “ASP.NET Empty Web Site” and give a solution name. For example: I took an example “Customer Bill”.



  • Right click on the Solution Explorer, go to Add option, select Add New Item option and select Web form and click Add option.

Step 2

After adding the Web form, we have to design my web page, according to my requirement, so here, I took a table and within a table, five textboxes, one button and a grid view.

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2.   
  3.     <!DOCTYPE html>  
  4.   
  5.     <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7.     <head runat="server">  
  8.         <title></title>  
  9.     </head>  
  10.   
  11.     <body>  
  12.         <form id="form1" runat="server">  
  13.             <div align="center">  
  14.                 <h1 style="color:green">Welcome To CURD Operation Without Database</h1>  
  15.                 <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">  
  16.                     <table>  
  17.                         <tr>  
  18.                             <td></td>  
  19.                             <th>Emp ID:  
  20.                             </th>  
  21.                             <th>Emp Name:  
  22.                             </th>  
  23.                             <th>Dept Name :  
  24.                             </th>  
  25.                             <th>Emp Address :  
  26.                             </th>  
  27.                             <th>Emp Salary:  
  28.                             </th>  
  29.                         </tr>  
  30.                         <tr>  
  31.                             <td></td>  
  32.                             <td>  
  33.                                 <asp:TextBox ID="txtEmpId" runat="server" Width="120px"></asp:TextBox>  
  34.   
  35.                             </td>  
  36.                             <td>  
  37.                                 <asp:TextBox ID="txtEmpName" runat="server" Width="120px"></asp:TextBox>  
  38.                             </td>  
  39.                             <td>  
  40.                                 <asp:TextBox ID="txtDeptName" runat="server" Width="120px"></asp:TextBox>  
  41.                             </td>  
  42.                             <td>  
  43.                                 <asp:TextBox ID="txtEmpAddress" runat="server" Width="120px"></asp:TextBox>  
  44.                             </td>  
  45.                             <td>  
  46.                                 <asp:TextBox ID="txtEmpSalary" runat="server" Width="120px"></asp:TextBox>  
  47.                             </td>  
  48.                         </tr>  
  49.                         <tr>  
  50.   
  51.                             <td colspan="6" align="right">  
  52.                                 <asp:Button ID="AddEmpDetails" runat="server" Style="color: White" Text="Add Employee" OnClick="AddEmpDetails_Click" BackColor="#00248E" />  
  53.                             </td>  
  54.                         </tr>  
  55.                     </table>  
  56.   
  57.                     <div style="margin-left: 10px; margin-top: 10px">  
  58.                         <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">  
  59.                             <Columns>  
  60.                                 <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" HeaderText="Operation" ItemStyle-Width="120px" />  
  61.   
  62.                                 <asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Id" DataField="EmpId">  
  63.                                     <HeaderStyle Width="120px"></HeaderStyle>  
  64.                                 </asp:BoundField>  
  65.                                 <asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Name" DataField="EmpName">  
  66.                                     <HeaderStyle Width="120px"></HeaderStyle>  
  67.                                 </asp:BoundField>  
  68.                                 <asp:BoundField HeaderStyle-Width="120px" HeaderText="Dept Name" DataField="DeptName">  
  69.                                     <HeaderStyle Width="120px"></HeaderStyle>  
  70.                                 </asp:BoundField>  
  71.                                 <asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Address" DataField="EmpAddress">  
  72.                                     <HeaderStyle Width="120px"></HeaderStyle>  
  73.                                 </asp:BoundField>  
  74.                                 <asp:BoundField HeaderStyle-Width="120px" HeaderText="Emp Salary" DataField="EmpSalary">  
  75.                                     <HeaderStyle Width="120px"></HeaderStyle>  
  76.                                 </asp:BoundField>  
  77.                             </Columns>  
  78.                             <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />  
  79.                             <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />  
  80.                             <PagerStyle BackColor="#99CCCC" ForeColor="#003399" Horizontal />  
  81.                             <RowStyle BackColor="White" ForeColor="#003399" />  
  82.                             <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />  
  83.                             <SortedAscendingCellStyle BackColor="#EDF6F6" />  
  84.                             <SortedAscendingHeaderStyle BackColor="#0D4AC4" />  
  85.                             <SortedDescendingCellStyle BackColor="#D6DFDF" />  
  86.                             <SortedDescendingHeaderStyle BackColor="#002876" />  
  87.                         </asp:GridView>  
  88.                     </div>  
  89.                     <br />  
  90.                 </div>  
  91.             </div>  
  92.         </form>  
  93.     </body>  
  94.   
  95. </html>  
Step 3

Now, we have to perform logic for CRUD operation, so for this, right click the design page and select view code.

Here, I have used datatable for the dynamic generated table.

Default.aspx.cs

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. public partial class Default2: System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (!IsPostBack)  
  13.         {  
  14.             DefaultEmpRecord();  
  15.         }  
  16.     }  
  17.     private void DefaultEmpRecord()  
  18.     {  
  19.         //creating dataTable   
  20.         DataTable dt = new DataTable();  
  21.         DataRow dr;  
  22.         dt.TableName = "EmployeeDetails";  
  23.         dt.Columns.Add(new DataColumn("EmpId"typeof(string)));  
  24.         dt.Columns.Add(new DataColumn("EmpName"typeof(string)));  
  25.         dt.Columns.Add(new DataColumn("DeptName"typeof(string)));  
  26.         dt.Columns.Add(new DataColumn("EmpAddress"typeof(string)));  
  27.         dt.Columns.Add(new DataColumn("EmpSalary"typeof(string)));  
  28.         dr = dt.NewRow();  
  29.         dt.Rows.Add(dr);  
  30.         //saving databale into viewstate   
  31.         ViewState["EmployeeDetails"] = dt;  
  32.         //bind Gridview   
  33.         GridView1.DataSource = dt;  
  34.         GridView1.DataBind();  
  35.     }  
  36.     private void AddNewRecordRowToGrid()  
  37.     {  
  38.         // check view state is not null   
  39.         if (ViewState["EmployeeDetails"] != null)  
  40.         {  
  41.             //get datatable from view state   
  42.             DataTable dtCurrentTable = (DataTable) ViewState["EmployeeDetails"];  
  43.             DataRow drCurrentRow = null;  
  44.             if (dtCurrentTable.Rows.Count > 0)  
  45.             {  
  46.                 for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)  
  47.                 {  
  48.                     //add each row into data table   
  49.                     drCurrentRow = dtCurrentTable.NewRow();  
  50.                     drCurrentRow["EmpId"] = txtEmpId.Text;  
  51.                     drCurrentRow["EmpName"] = txtEmpName.Text;  
  52.                     drCurrentRow["DeptName"] = txtDeptName.Text;  
  53.                     drCurrentRow["EmpAddress"] = txtEmpAddress.Text;  
  54.                     drCurrentRow["EmpSalary"] = txtEmpSalary.Text;  
  55.                 }  
  56.                 //Remove initial blank row   
  57.                 if (dtCurrentTable.Rows[0][0].ToString() == "")  
  58.                 {  
  59.                     dtCurrentTable.Rows[0].Delete();  
  60.                     dtCurrentTable.AcceptChanges();  
  61.                 }  
  62.                 //add created Rows into dataTable   
  63.                 dtCurrentTable.Rows.Add(drCurrentRow);  
  64.                 //Bind Gridview with latest Row   
  65.                 GridView1.DataSource = dtCurrentTable;  
  66.                 GridView1.DataBind();  
  67.             }  
  68.         }  
  69.     }  
  70.     protected void AddEmpDetails_Click(object sender, EventArgs e)  
  71.     {  
  72.         AddNewRecordRowToGrid();  
  73.     }  
  74.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  75.     {  
  76.         DataTable dt = new DataTable();  
  77.         dt.Columns.Add(new DataColumn("EmpId"typeof(string)));  
  78.         dt.Columns.Add(new DataColumn("EmpName"typeof(string)));  
  79.         dt.Columns.Add(new DataColumn("DeptName"typeof(string)));  
  80.         dt.Columns.Add(new DataColumn("EmpAddress"typeof(string)));  
  81.         dt.Columns.Add(new DataColumn("EmpSalary"typeof(string)));  
  82.         string Id = string.Empty;  
  83.         string Name = string.Empty;  
  84.         string DeptName = string.Empty;  
  85.         string EmpAddress = string.Empty;  
  86.         string EmpSalary = string.Empty;  
  87.         for (int i = 0; i < GridView1.Rows.Count; i++)  
  88.         {  
  89.             GridViewRow row = (GridViewRow) GridView1.Rows[i];  
  90.             if (i != e.RowIndex)  
  91.             {  
  92.                 Id = row.Cells[1].Text;  
  93.                 Name = row.Cells[2].Text;  
  94.                 DeptName = row.Cells[3].Text;  
  95.                 EmpAddress = row.Cells[4].Text;  
  96.                 EmpSalary = row.Cells[5].Text;  
  97.             }  
  98.             else  
  99.             {  
  100.                 Id = ((TextBox) row.Cells[1].Controls[0]).Text;  
  101.                 Name = ((TextBox) row.Cells[2].Controls[0]).Text;  
  102.                 DeptName = ((TextBox) row.Cells[3].Controls[0]).Text;  
  103.                 EmpAddress = ((TextBox) row.Cells[4].Controls[0]).Text;  
  104.                 EmpSalary = ((TextBox) row.Cells[5].Controls[0]).Text;  
  105.             }  
  106.             DataRow dr = dt.NewRow();  
  107.             dr["EmpId"] = Id;  
  108.             dr["EmpName"] = Name;  
  109.             dr["DeptName"] = DeptName;  
  110.             dr["EmpAddress"] = EmpAddress;  
  111.             dr["EmpSalary"] = EmpSalary;  
  112.             dt.Rows.Add(dr);  
  113.         }  
  114.         GridView1.EditIndex = -1;  
  115.         GridView1.DataSource = dt;  
  116.         GridView1.DataBind();  
  117.     }  
  118.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  119.     {  
  120.         {  
  121.             DataTable dt = new DataTable();  
  122.             dt.Columns.Add(new DataColumn("EmpId"typeof(string)));  
  123.             dt.Columns.Add(new DataColumn("EmpName"typeof(string)));  
  124.             dt.Columns.Add(new DataColumn("DeptName"typeof(string)));  
  125.             dt.Columns.Add(new DataColumn("EmpAddress"typeof(string)));  
  126.             dt.Columns.Add(new DataColumn("EmpSalary"typeof(string)));  
  127.             string Id = string.Empty;  
  128.             string Name = string.Empty;  
  129.             string DeptName = string.Empty;  
  130.             string EmpAddress = string.Empty;  
  131.             string EmpSalary = string.Empty;  
  132.             for (int i = 0; i < GridView1.Rows.Count; i++)  
  133.             {  
  134.                 GridViewRow row = (GridViewRow) GridView1.Rows[i];  
  135.                 if (i != e.RowIndex)  
  136.                 {  
  137.                     Id = row.Cells[1].Text;  
  138.                     Name = row.Cells[2].Text;  
  139.                     DeptName = row.Cells[3].Text;  
  140.                     EmpAddress = row.Cells[4].Text;  
  141.                     EmpSalary = row.Cells[5].Text;  
  142.                     DataRow dr = dt.NewRow();  
  143.                     dr["EmpId"] = Id;  
  144.                     dr["EmpName"] = Name;  
  145.                     dr["DeptName"] = DeptName;  
  146.                     dr["EmpAddress"] = EmpAddress;  
  147.                     dr["EmpSalary"] = EmpSalary;  
  148.                     dt.Rows.Add(dr);  
  149.                 }  
  150.             }  
  151.             GridView1.EditIndex = -1;  
  152.             GridView1.DataSource = dt;  
  153.             GridView1.DataBind();  
  154.         }  
  155.     }  
  156.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  157.     {  
  158.         GridView1.EditIndex = -1;  
  159.         BindEmpDetails();  
  160.     }  
  161.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  162.     {  
  163.         GridView1.EditIndex = e.NewEditIndex;  
  164.         BindEmpDetails();  
  165.     }  
  166.     private void BindEmpDetails()  
  167.     {  
  168.         if (ViewState["EmployeeDetails"] != null)  
  169.         {  
  170.             //get datatable from view state   
  171.             DataTable dtCurrentTable = (DataTable) ViewState["EmployeeDetails"];  
  172.             DataRow drCurrentRow = null;  
  173.             if (dtCurrentTable.Rows.Count > 0)  
  174.             {  
  175.                 for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)  
  176.                 {  
  177.                     //add each row into data table   
  178.                     drCurrentRow = dtCurrentTable.NewRow();  
  179.                     drCurrentRow["EmpId"] = txtEmpId.Text;  
  180.                     drCurrentRow["EmpName"] = txtDeptName.Text;  
  181.                     drCurrentRow["DeptName"] = txtDeptName.Text;  
  182.                     drCurrentRow["EmpAddress"] = txtEmpAddress.Text;  
  183.                     drCurrentRow["EmpSalary"] = txtEmpSalary.Text;  
  184.                 }  
  185.                 //Remove initial blank row   
  186.                 if (dtCurrentTable.Rows[0][0].ToString() == "")  
  187.                 {  
  188.                     dtCurrentTable.Rows[0].Delete();  
  189.                     dtCurrentTable.AcceptChanges();  
  190.                 }  
  191.                 //Bind Gridview with latest Row   
  192.                 GridView1.DataSource = dtCurrentTable;  
  193.                 GridView1.DataBind();  
  194.             }  
  195.         }  
  196.     }  
  197. }  
Step 4

Now, we finally run the project and see the output.

Output



Fill all the Text-boxes and click Add Employee button to see the result.

Similarly, I added five records here, for an example.



Afterwards, click the Edit button to update the record.



After making the changes, click update button.



Afterwards, click delete button.

 

Up Next
    Ebook Download
    View all
    Learn
    View all