Step by step Perform CRUD Operations in Asp.net with the use of Entity Framework
Through-out the article we will learn step-by-step: How to perform crud operations in Asp.net with using Entity Framework(EDMX). We will discuss crud operations with usage of pure Sql server. I have mention the code for Save,Edit,delete given below.
Abstract
Through-out the article we will learn step-by-step: How to perform crud operations in Asp.net and C# without using Entity Framework. We will discuss crud operations with usage of pure Sql server. Introduction
In these days, Dotnet is most growing framework and as a developer I am curious to know and learn
about the new features of this framework.
From last couple of days I was wondering how can I perform CRUD operations using pure sql queries. For the same I have gone through various forums etc. Unfortunately, I did saw very few posts/articles for the same.
Finally, I tried and came with a solution to perform CRUD operations without the use of Entity Framework. In this article, I am going to share my findings with all of you.Why Step-by-Step
Ah! This question came to my mind while I was finding the solution of my problem, I jotted down few points and finally
called them as a steps to perform CRUD Operations.Pre-requisite
To implement the solution and to feel the taste of code, you should:What we are waiting for?
So, what we are waiting for here, lets get started. Just following these step(s) and we will done!Step1: Create Asp.net Project
Launch your visual studio and perform following action (Refer to snapshot)
File->New->Select the new project ->Web Application->Select the Web Template.Step2:Create WebForms and Methods
Right click the Project then Add-> Add New Item Select Web Form extension .aspx-> give the Filename, then add methods what do you need and then right click to create a new view.
Step3:Bind the Records using
In your view, write or copy/paste code, mentioned in following code-snippet:
Server side Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
Client Side Code
<asp:GridView ID="Grd1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField HeaderText="Empno" DataField="Empno" />
<asp:BoundField HeaderText="EmpName" DataField="EmpName" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" runat="server"
ImageUrl="~/Images/Edit.jpg" OnClick="ibtnEdit_Click" Width="20px" Height="20px" />
<asp:ImageButton ID="ibtnDelete" runat="server"
ImageUrl="~/Images/Delete.jpg"
OnClientClick="javascript:return confirm('Do you want to delete it?');"
OnClick="ibtnDelete_Click" Width="20px" Height="20px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Following code invoke the Inserted Records to database.
if (Btnsubmit.Text == "Insert")
{
tblem.EmpNo = TxtEmpno.Text;
tblem.EmpName = TxtEmpName.Text;
DB.TblEmps.Add(tblem);
DB.SaveChanges();
}
Following code-snippet defines How to select corresponding record and then to update them.
TblEmp tblem = new TblEmp();
if (Btnsubmit.Text == "Update")
{
using (TestEntities testdb = new TestEntities())
{
TblEmp tbemp = DB.TblEmps.Find(Convert.ToInt32(Session["UserId"]));
tbemp.EmpName = TxtEmpName.Text;
tbemp.EmpNo = TxtEmpno.Text;
testdb.Entry(tbemp).State = EntityState.Modified;
testdb.SaveChanges();
}
}
Now, Fetch the records from database by passing parameters and without parameters.
private void FillGrid()
{
Grd1.DataSource = DB.TblEmps.ToList();
Grd1.DataBind();
}
Following code-snippet describing how to delete the corresponding records:
TblEmp student =DB.TblEmps.Find(1);
DB.TblEmps.Remove(student);
DB.SaveChanges();
Following code-snippet describing how to Edit using GridView the corresponding records:
GridViewRow gvRow = (GridViewRow)((ImageButton)sender).NamingContainer;
Int32 UserId = Convert.ToInt32(Grd1.DataKeys[gvRow.RowIndex].Value);
Session["UserId"] = UserId;
TblEmp tbemp = DB.TblEmps.Find(UserId);
TxtEmpno.Text = tbemp.EmpNo;
TxtEmpName.Text = tbemp.EmpName;
Btnsubmit.Text = "Update";
Write or copy/paste code to your FileName.aspx view, mentioned in the following code-snippet:
<table>
<tr>
<td>
Empno
</td>
<td>
<asp:TextBox ID="TxtEmpno" runat ="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
EmpName
</td>
<td>
<asp:TextBox ID="TxtEmpName" runat ="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Btnsubmit" runat ="server" Text="Submit" OnClick="Btnsubmit_Click" />
</td>
</tr>
</table>
<asp:GridView ID="Grd1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField HeaderText="Empno" DataField="Empno" />
<asp:BoundField HeaderText="EmpName" DataField="EmpName" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" runat="server"
ImageUrl="~/Images/Edit.jpg" OnClick="ibtnEdit_Click" Width="20px" Height="20px" />
<asp:ImageButton ID="ibtnDelete" runat="server"
ImageUrl="~/Images/Delete.jpg"
OnClientClick="javascript:return confirm('Do you want to delete it?');"
OnClick="ibtnDelete_Click" Width="20px" Height="20px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
Following is the Generated Connection string Design view of our CRUD Operations:
Conclusion
In this whole article, we discussed about CRUD operations using MVC5.
1. Create a new project
2. Choose correct template
3. Add Web Forms
4. Add various operations
Hope, you find this article helpful.