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.
Step 2
Select Web Application Template.
Step 3
Add new item.
Right click in project solution => Add => New Item
Step 4
Paste the below Html code under the body tag.
- <form id="form1" runat="server">
- <div style="display: flex; justify-content: center;">
- <table border="1">
- <tr>
- <td>Employee ID</td>
- <td>
- <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="rfEmpId" runat="server" ControlToValidate="txtID" ErrorMessage="*" ForeColor="Red" ValidationGroup="grpemp"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>Employee Name</td>
- <td>
- <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="rfEmpName" runat="server" ControlToValidate="txtName" ErrorMessage="*" ForeColor="Red" ValidationGroup="grpemp"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>Gender</td>
- <td>
- <asp:DropDownList ID="ddlgender" runat="server">
- <asp:ListItem>--Select--</asp:ListItem>
- <asp:ListItem>Male</asp:ListItem>
- <asp:ListItem>Female</asp:ListItem>
- </asp:DropDownList>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="rfEmpgender" runat="server" InitialValue="--Select--" ControlToValidate="ddlgender" ErrorMessage="*" ForeColor="Red" ValidationGroup="grpemp"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr style="text-align: center;">
- <td colspan="2">
- <asp:Button ID="btnAdd" runat="server" Text="ADD" Style="cursor: pointer;" OnClick="btnAdd_Click" ValidationGroup="grpemp" CausesValidation="true" />
- </td>
- </tr>
- </table>
- </div>
- <br />
- <br />
- <div style="display: flex; justify-content: center;">
- <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">
- <Columns>
- <asp:TemplateField HeaderText="SELECT">
- <itemtemplate>
- <asp:LinkButton ID="LnkSelect" runat="server" CommandName="Select">Select</asp:LinkButton>
- </itemtemplate>
- </asp:TemplateField>
-
- <asp:BoundField DataField="ID" HeaderText="EMPLOYEE ID" />
- <asp:BoundField DataField="Name" HeaderText="EMPLOYEE NAME" />
- <asp:BoundField DataField="Gender" HeaderText="GENDER" />
-
- <asp:TemplateField HeaderText="DELETE">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you sure to delete this employee?');">Delete</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
- <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
- <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
- <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
- <%--<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%>
- <SortedAscendingCellStyle BackColor="#FFF1D4" />
- <SortedAscendingHeaderStyle BackColor="#B95C30" />
- <SortedDescendingCellStyle BackColor="#F1E5CE" />
- <SortedDescendingHeaderStyle BackColor="#93451F" />
- </asp:GridView>
- </div>
- </form>
Step 5
Replace auto generated class code with the below code.
- public partial class CRUD : System.Web.UI.Page
- {
- DataTable dtEmp = new DataTable("Employee");
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- if (Page.IsValid)
- {
- if (btnAdd.Text == "ADD")
- {
- if (gvemployee.Rows.Count == 0)
- {
- dtEmp.Columns.Add("ID", typeof(String));
- dtEmp.Columns.Add("Name", typeof(String));
- dtEmp.Columns.Add("Gender", typeof(String));
-
- DataRow dr = dtEmp.NewRow();
- dr["ID"] = txtID.Text;
- dr["Name"] = txtName.Text;
- dr["Gender"] = ddlgender.SelectedValue;
-
- dtEmp.Rows.Add(dr);
- ViewState["EmployeeTable"] = dtEmp;
- gvemployee.DataSource = dtEmp;
- gvemployee.DataBind();
- }
- else
- {
- dtEmp = (DataTable)ViewState["EmployeeTable"];
- DataRow dr;
-
- dr = dtEmp.Select("ID='" + txtID.Text + "'").FirstOrDefault();
- if (dr != null)
- {
- ClientScript.RegisterStartupScript(this.GetType(), "employeealert", "alert('Employee ID already exist');", true);
- return;
- }
-
- dr = dtEmp.NewRow();
- dr["ID"] = txtID.Text;
- dr["Name"] = txtName.Text;
- dr["Gender"] = ddlgender.SelectedValue;
-
- dtEmp.Rows.Add(dr);
- ViewState["EmployeeTable"] = dtEmp;
- gvemployee.DataSource = dtEmp;
- gvemployee.DataBind();
-
- }
- }
- else
- {
- dtEmp = (DataTable)ViewState["EmployeeTable"];
-
- DataRow dr = dtEmp.Select("ID='" + txtID.Text + "'").FirstOrDefault();
- if (dr != null)
- {
- dr["Name"] = txtName.Text;
- dr["Gender"] = ddlgender.SelectedValue;
- }
-
- gvemployee.DataSource = dtEmp;
- gvemployee.DataBind();
-
- txtID.ReadOnly = false;
- btnAdd.Text = "ADD";
- }
-
- Clear();
- }
- }
-
- protected void gvemployee_SelectedIndexChanged(object sender, EventArgs e)
- {
- txtID.Text = gvemployee.SelectedRow.Cells[1].Text;
- txtName.Text = gvemployee.SelectedRow.Cells[2].Text;
- ddlgender.SelectedValue = gvemployee.SelectedRow.Cells[3].Text;
-
- txtID.ReadOnly = true;
- btnAdd.Text = "Update";
- }
-
- public void Clear()
- {
- txtID.Text = "";
- txtName.Text = "";
- ddlgender.SelectedIndex = 0;
- }
-
- protected void gvemployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- dtEmp = (DataTable)ViewState["EmployeeTable"];
-
- dtEmp.Rows.RemoveAt(e.RowIndex);
-
- gvemployee.DataSource = dtEmp;
- gvemployee.DataBind();
- }
- }
Step 5
Run application.
Fill in all fields and click on ADD.
Add more data
Edit any row data by selecting and then press update button.
I am going to Edit EMP2 data from name Atul to Atul Pethe
Now the name is changed from Atul to Atul Pethe
Delete any row data by deleting Link Button,
I am going to delete EMP3 data.
Now EMP3 data is deleted.
I hope this helps.
Thank you.