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

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