Static CRUD Operation In C#

Introduction to CRUD

 
CRUD - Create, Read, Update And Delete
 
In this article, you will learn how to make a static application that performs Select, Add, Update & Delete operations. 
 
Let's start.
 
Step 1 - Add New Project
 
Open Visual Studio 2017 => Click Add Project => Select Web Application Template => Fill all required details. 
 
Static CRUD Operation In C#
 
Step 2

Select Web Application Template.
 
Static CRUD Operation In C#
 
Step 3
 
Add new item.

Right click in project solution => Add => New Item
 
Static CRUD Operation In C#
 
Step 4
 
Paste the below Html code under the body tag.
  1. <form id="form1" runat="server">    
  2.     <div style="display: flex; justify-content: center;">    
  3.         <table border="1">    
  4.             <tr>    
  5.                 <td>Employee ID</td>    
  6.                 <td>    
  7.                     <asp:TextBox ID="txtID" runat="server"></asp:TextBox>    
  8.                 </td>    
  9.                 <td>    
  10.                     <asp:RequiredFieldValidator ID="rfEmpId" runat="server" ControlToValidate="txtID" ErrorMessage="*" ForeColor="Red" ValidationGroup="grpemp"></asp:RequiredFieldValidator>    
  11.                 </td>    
  12.             </tr>    
  13.             <tr>    
  14.                 <td>Employee Name</td>    
  15.                 <td>    
  16.                     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>    
  17.                 </td>    
  18.                 <td>    
  19.                     <asp:RequiredFieldValidator ID="rfEmpName" runat="server" ControlToValidate="txtName" ErrorMessage="*"  ForeColor="Red"  ValidationGroup="grpemp"></asp:RequiredFieldValidator>    
  20.                 </td>    
  21.             </tr>    
  22.             <tr>    
  23.                 <td>Gender</td>    
  24.                 <td>    
  25.                     <asp:DropDownList ID="ddlgender" runat="server">    
  26.                         <asp:ListItem>--Select--</asp:ListItem>    
  27.                         <asp:ListItem>Male</asp:ListItem>    
  28.                         <asp:ListItem>Female</asp:ListItem>    
  29.                     </asp:DropDownList>    
  30.                 </td>    
  31.                 <td>    
  32.                     <asp:RequiredFieldValidator ID="rfEmpgender" runat="server" InitialValue="--Select--" ControlToValidate="ddlgender" ErrorMessage="*"  ForeColor="Red"  ValidationGroup="grpemp"></asp:RequiredFieldValidator>    
  33.                 </td>    
  34.             </tr>    
  35.             <tr style="text-align: center;">    
  36.                 <td colspan="2">    
  37.                     <asp:Button ID="btnAdd" runat="server" Text="ADD" Style="cursor: pointer;" OnClick="btnAdd_Click"  ValidationGroup="grpemp" CausesValidation="true" />    
  38.                 </td>    
  39.             </tr>    
  40.         </table>    
  41.     </div>    
  42.     <br />    
  43.     <br />    
  44.     <div style="display: flex; justify-content: center;">    
  45.         <asp:GridView ID="gvemployee" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" OnSelectedIndexChanged="gvemployee_SelectedIndexChanged" OnRowDeleting="gvemployee_RowDeleting" AutoGenerateColumns="false">    
  46.             <Columns>    
  47.                 <asp:TemplateField HeaderText="SELECT">    
  48.                     <itemtemplate>    
  49.                         <asp:LinkButton ID="LnkSelect" runat="server" CommandName="Select">Select</asp:LinkButton>    
  50.                     </itemtemplate>    
  51.                 </asp:TemplateField>    
  52.   
  53.                 <asp:BoundField DataField="ID" HeaderText="EMPLOYEE ID" />    
  54.                 <asp:BoundField DataField="Name" HeaderText="EMPLOYEE NAME" />    
  55.                 <asp:BoundField DataField="Gender" HeaderText="GENDER" />    
  56.   
  57.                 <asp:TemplateField HeaderText="DELETE">    
  58.                     <ItemTemplate>    
  59.                         <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you sure to delete this employee?');">Delete</asp:LinkButton>    
  60.                     </ItemTemplate>    
  61.                 </asp:TemplateField>  
  62.             </Columns>    
  63.             <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />    
  64.             <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />    
  65.             <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />    
  66.             <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />    
  67.             <%--<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%>    
  68.             <SortedAscendingCellStyle BackColor="#FFF1D4" />    
  69.             <SortedAscendingHeaderStyle BackColor="#B95C30" />    
  70.             <SortedDescendingCellStyle BackColor="#F1E5CE" />    
  71.             <SortedDescendingHeaderStyle BackColor="#93451F" />    
  72.         </asp:GridView>    
  73.     </div>    
  74. </form> 
Step 5
 
Replace auto generated class code with the below code.
  1. public partial class CRUD : System.Web.UI.Page    
  2. {    
  3.     DataTable dtEmp = new DataTable("Employee");    
  4.     protected void Page_Load(object sender, EventArgs e)    
  5.     {    
  6.   
  7.     }    
  8.   
  9.     protected void btnAdd_Click(object sender, EventArgs e)    
  10.     {    
  11.         if (Page.IsValid)    
  12.         {    
  13.             if (btnAdd.Text == "ADD")    
  14.             {    
  15.                 if (gvemployee.Rows.Count == 0)    
  16.                 {    
  17.                     dtEmp.Columns.Add("ID"typeof(String));    
  18.                     dtEmp.Columns.Add("Name"typeof(String));    
  19.                     dtEmp.Columns.Add("Gender"typeof(String));    
  20.   
  21.                     DataRow dr = dtEmp.NewRow();    
  22.                     dr["ID"] = txtID.Text;    
  23.                     dr["Name"] = txtName.Text;    
  24.                     dr["Gender"] = ddlgender.SelectedValue;    
  25.   
  26.                     dtEmp.Rows.Add(dr);    
  27.                     ViewState["EmployeeTable"] = dtEmp;    
  28.                     gvemployee.DataSource = dtEmp;    
  29.                     gvemployee.DataBind();    
  30.                 }    
  31.                 else    
  32.                 {    
  33.                     dtEmp = (DataTable)ViewState["EmployeeTable"];    
  34.                     DataRow dr;    
  35.   
  36.                     dr = dtEmp.Select("ID='" + txtID.Text + "'").FirstOrDefault();    
  37.                     if (dr != null)    
  38.                     {    
  39.                         ClientScript.RegisterStartupScript(this.GetType(), "employeealert""alert('Employee ID already exist');"true);    
  40.                         return;    
  41.                     }    
  42.   
  43.                     dr = dtEmp.NewRow();    
  44.                     dr["ID"] = txtID.Text;    
  45.                     dr["Name"] = txtName.Text;    
  46.                     dr["Gender"] = ddlgender.SelectedValue;    
  47.   
  48.                     dtEmp.Rows.Add(dr);    
  49.                     ViewState["EmployeeTable"] = dtEmp;    
  50.                     gvemployee.DataSource = dtEmp;    
  51.                     gvemployee.DataBind();    
  52.   
  53.                 }    
  54.             }    
  55.             else    
  56.             {    
  57.                 dtEmp = (DataTable)ViewState["EmployeeTable"];    
  58.   
  59.                 DataRow dr = dtEmp.Select("ID='" + txtID.Text + "'").FirstOrDefault();    
  60.                 if (dr != null)    
  61.                 {    
  62.                     dr["Name"] = txtName.Text;    
  63.                     dr["Gender"] = ddlgender.SelectedValue;    
  64.                 }    
  65.   
  66.                 gvemployee.DataSource = dtEmp;    
  67.                 gvemployee.DataBind();    
  68.   
  69.                 txtID.ReadOnly = false;    
  70.                 btnAdd.Text = "ADD";    
  71.             }    
  72.   
  73.             Clear();    
  74.         }    
  75.     }    
  76.   
  77.     protected void gvemployee_SelectedIndexChanged(object sender, EventArgs e)    
  78.     {    
  79.         txtID.Text = gvemployee.SelectedRow.Cells[1].Text;    
  80.         txtName.Text = gvemployee.SelectedRow.Cells[2].Text;    
  81.         ddlgender.SelectedValue = gvemployee.SelectedRow.Cells[3].Text;    
  82.   
  83.         txtID.ReadOnly = true;    
  84.         btnAdd.Text = "Update";    
  85.     }    
  86.   
  87.     public void Clear()    
  88.     {    
  89.         txtID.Text = "";    
  90.         txtName.Text = "";    
  91.         ddlgender.SelectedIndex = 0;    
  92.     }    
  93.   
  94.     protected void gvemployee_RowDeleting(object sender, GridViewDeleteEventArgs e)    
  95.     {    
  96.         dtEmp = (DataTable)ViewState["EmployeeTable"];    
  97.   
  98.         dtEmp.Rows.RemoveAt(e.RowIndex);    
  99.   
  100.         gvemployee.DataSource = dtEmp;    
  101.         gvemployee.DataBind();    
  102.     }    
  103. } 
Step 5
 
Run application.
 
Static CRUD Operation In C#
 
Fill in all fields and click on ADD. 
 
Static CRUD Operation In C#
 
Add more data
 
Static CRUD Operation In C#
 
Edit any row data by selecting and then press update button.
 
I am going to Edit EMP2 data from name Atul to Atul Pethe
 
Static CRUD Operation In C#
 
Now the name is changed from Atul to Atul Pethe 
 
Static CRUD Operation In C#
 
Delete any row data by deleting Link Button,
 
I am going to delete EMP3 data.
 
Static CRUD Operation In C#
 
Now EMP3 data is deleted.
 
Static CRUD Operation In C#
 
I hope this helps. 
 
Thank you.


Similar Articles