Perform CRUD Operations in GridView Without Using Database in ASP.NET

In this article, I will explain how to insert, edit, update and delete records in a GridView without a database in ASP.NET.
  1. Go to Start, then All Programs and open Microsoft Visual Studio 2013.

  2. Now, click on File, then select New Project and click on Visual C#. Then select ASP.NET Web Application, Empty and click on OK.

  3. Provide the web application name and location as you wish. I named my web application InsertEditUpdateDeleteGridviewWithOutDB.

  4. Now the project will be opened. Right-click on the web application name, add a New Item and select Web Form. Then click on Add.

  5. Add the following code between the <form> tags of the WebForm1.aspx page.
    1. <div align="center">    
    2.     <table>    
    3.         <tr>    
    4.             <td>EmpId</td>    
    5.             <td>    
    6.                 <asp:TextBox ID="txtEmpId" runat="server" />    
    7.             </td>    
    8.         </tr>    
    9.         <tr>    
    10.             <td>EmpName</td>    
    11.             <td>    
    12.                 <asp:TextBox ID="txtEmpName" runat="server" />    
    13.             </td>    
    14.         </tr>    
    15.         <tr>    
    16.             <td>EmpSalary</td>    
    17.             <td>    
    18.                 <asp:TextBox ID="txtEmpSalary" runat="server" />    
    19.             </td>    
    20.         </tr>    
    21.         <tr>    
    22.             <td>Department</td>    
    23.             <td>    
    24.                 <asp:TextBox ID="txtDept" runat="server" />    
    25.             </td>    
    26.         </tr>    
    27.     </table>    
    28.     <br />    
    29.     <asp:Button ID="btnInsert" runat="server" Text="Insert" Height="30" Width="70" OnClick="btnInsert_Click" OnClientClick="return validate();" />    
    30.     <br /><br />    
    31.     <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing">    
    32.         <Columns>    
    33.             <asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="true" />    
    34.             <asp:BoundField DataField="EmpName" HeaderText="EmpName" />    
    35.             <asp:BoundField DataField="EmpSalary" HeaderText="EmpSalary" />    
    36.             <asp:BoundField DataField="Dept" HeaderText="Department" />    
    37.             <asp:TemplateField>    
    38.                 <ItemTemplate>    
    39.                     <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />    
    40.                     <asp:LinkButton Text="Delete" runat="server" OnClick="OnDelete" />    
    41.                 </ItemTemplate>    
    42.                 <EditItemTemplate>    
    43.                     <asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />    
    44.                     <asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />    
    45.                 </EditItemTemplate>    
    46.             </asp:TemplateField>    
    47.         </Columns>    
    48.     </asp:GridView>    
    49. </div>     
6. Add the following code to the page load event method of the WebForm1.aspx.cs page.
  1. protected void Page_Load(object sender, EventArgs e)    
  2. {    
  3.     if (!this.IsPostBack)    
  4.     {    
  5.         DataTable dt = new DataTable();    
  6.         dt.Columns.Add("EmpId");    
  7.         dt.Columns.Add("EmpName");    
  8.         dt.Columns.Add("EmpSalary");    
  9.         dt.Columns.Add("Dept");    
  10.         dt.Rows.Add(1, "JACKSON", 75000, "RESEARCH");    
  11.         dt.Rows.Add(2, "JOHNSON", 18000, "ACCOUNTING");    
  12.         dt.Rows.Add(3, "GRANT", 32000, "SALES");    
  13.         dt.Rows.Add(4, "ADAMS", 34000, "OPERATIONS");    
  14.         ViewState["dt"] = dt;    
  15.         BindGrid();    
  16.     }    
  17. }    
  18.   
  19. protected void BindGrid()    
  20. {    
  21.     GridView1.DataSource = ViewState["dt"as DataTable;    
  22.     GridView1.DataBind();    
  23. }   
7. Add the following code to the GridView1_RowEditing event method of the WebForm1.aspx.cs page.
  1. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  2. {  
  3.      GridView1.EditIndex = e.NewEditIndex;  
  4.      BindGrid();  
  5. }  
8. Add the following code to the OnUpdate event method of the WebForm1.aspx.cs page.
  1. protected void OnUpdate(object sender, EventArgs e)  
  2. {  
  3.      GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;  
  4.      string empname=((TextBox)row.Cells[1].Controls[0]).Text;  
  5.      int empsal = Convert.ToInt32(((TextBox)row.Cells[2].Controls[0]).Text);  
  6.      string dept = ((TextBox)row.Cells[3].Controls[0]).Text;  
  7.   
  8.      DataTable dt = ViewState["dt"as DataTable;  
  9.      dt.Rows[row.RowIndex]["EmpName"] = empname;  
  10.      dt.Rows[row.RowIndex]["EmpSalary"] = empsal;  
  11.      dt.Rows[row.RowIndex]["Dept"] = dept;  
  12.      ViewState["dt"] = dt;  
  13.      GridView1.EditIndex = -1;  
  14.      BindGrid();  
  15. }  
9. Add the following code to the OnCancel event method of the WebForm1.aspx.cs page.
  1. protected void OnCancel(object sender, EventArgs e)  
  2. {  
  3.       GridView1.EditIndex = -1;  
  4.       BindGrid();  
  5. }  
10. Add the following code to the OnDelete event method of the WebForm1.aspx.cs page.
  1. protected void OnDelete(object sender, EventArgs e)  
  2. {  
  3.      GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;  
  4.      DataTable dt = ViewState["dt"as DataTable;  
  5.      dt.Rows.RemoveAt(row.RowIndex);  
  6.      ViewState["dt"] = dt;  
  7.      BindGrid();  
  8. }  
11. Add the following code to the btnInsert_Click event method of the WebForm1.aspx.cs page.
  1. protected void btnInsert_Click(object sender, EventArgs e)    
  2. {    
  3.     DataTable dt = ViewState["dt"as DataTable;    
  4.     dt.Rows.Add(txtEmpId.Text, txtEmpName.Text, txtEmpSalary.Text, txtDept.Text);    
  5.     ViewState["dt"] = dt;    
  6.     BindGrid();    
  7.     txtEmpId.Text = string.Empty;    
  8.     txtEmpName.Text = string.Empty;    
  9.     txtEmpSalary.Text = string.Empty;    
  10.     txtDept.Text = string.Empty;    
  11. }  
12. Add a <script> tag between the <head> tags and write the following code in it.
  1. <script type="text/javascript">    
  2.     function validate()    
  3.     {    
  4.         var empid = document.getElementById("txtEmpId").value;    
  5.         var empname = document.getElementById("txtEmpName").value;    
  6.         var empsalary = document.getElementById("txtEmpSalary").value;    
  7.         var dept = document.getElementById("txtDept").value;    
  8.         if(empid=="" || empname=="" || empsalary=="" || dept=="")    
  9.         {    
  10.             alert("Fill All the Fields");    
  11.             return false;    
  12.         }    
  13.         else    
  14.         {    
  15.             return true;    
  16.         }    
  17.     }    
  18. </script>  
13. Now run the application and you can see it in your browser.
 
 
14. Now, insert one record into the GridView.
 
 
15. Now, update the Name GRANT of the EmpId 3 record to GRANT ELLIOTT.
 
 
16. Now, delete EmpId 4 Record.


Similar Articles