Basic Operations Insert, Update and Delete of Data From SQL Server in ASP
Basic Operations Insert, Update and Delete of Data From SQL Server in ASP
Controller
Classes that handle incoming requests to the application, retrieve model
data, and then specify view templates that return a response to the client.
Model
Classes that represent the data of the application and that use validation
logic to enforce business rules for that data.
View
Template files that your application uses to dynamically generate HTML
responses.
Open SQL Management Studio 2005. Crate a database and name itInventory.
Design a database table like below:
Figure 1
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 1/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
[http://www.codeproject.com/KB/aspnet/ASPNETMVCApplication/ASPNETMVCApplic
ation.png]
Figure 2
Figure 3
The default project will be created. NowAdd Library Package Reference.
We will use a .NET Framework data-access technology known as the Entity
Framework. The Entity Framework (often referred to as EF) supports a
development paradigm called code-first. Code-first allows you to create
model objects by writing simple classes. You can then have the database
created on the fly from your classes, which enables a very clean and rapid
development workflow.
We'll start by using the NuGet package manager (automatically installed by
ASP.NET MVC 3) to add the EFCodeFirst library to the
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 2/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 4
In theAdd Library Package Referencewindow, clickonline.
Figure 5
Click theNuGet Official package source.
Figure 6
FindEFCodeFirst, click oninstall.
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 3/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 7
Click onI Accept. Entity Framework will install and will add a reference to the
project.
Figure 8
Click on theClosebutton.
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 4/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 9
Figure 10
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 5/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 11
Add the following properties in theBook.csfile:
namespace ASPNETMVCApplication.Models
{
public class Book
{
public Int64 Id { get; set; }
public String Title { get; set; }
public String ISBN { get; set; }
public Decimal Price { get; set; }
}
}
Add another class the way you did forBook.csand name itBookDBContext.cs.
Add aSystem.Data.Entitynamespace reference in
theBookDBContext.csfile.
using System.Data.Entity;
using System;
using System.Web;
using System.Data.Entity;
namespace ASPNETMVCApplication.Models
{
public class BookDBContext:DbContext
{
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 6/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
<connectionStrings>
<add name="BookDBContext"
connectionString="data source=Home-PC;
uid=sa;pwd=1234;Initial Catalog=Inventory"
providerName="System.Data.SqlClient" />
</connectionStrings>
Figure 12
Figure 13
It will add the following code:
namespace ASPNETMVCApplication.Controllers
{
public class BookController : Controller
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 7/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
{
//
// GET: /Book/
//
// GET: /Book/Details/5
//
// GET: /Book/Create
//
// POST: /Book/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Book/Edit/5
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 8/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
//
// POST: /Book/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Book/Delete/5
//
// POST: /Book/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
using System.Linq;
using ASPNETMVCApplication.Models;
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 9/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
return View(books.ToList());
}
This is the LINQ code for getting books andbooks.ToList()to display the
book list in the View.
Now right click onSolutionand selectBuildto build the solution.
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 10/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 14
It will add theIndex.cshtmlfile.
@model IEnumerable<ASPNETMVCApplication.Models.Book>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
Title
</th>
<th>
ISBN
</th>
<th>
Price
</th>
</tr>
</table>
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 11/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
@model ASPNETMVCApplication.Models.Book
@{
ViewBag.Title = "Create";
}
<h2>Create</h2><script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"></script><script
src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript"></script>@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Book</legend><div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ISBN)
</div>
<div class="editor-field">
@Html.EditorFor(model =>
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
Book book = new Book();
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 12/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
if (ModelState.IsValid)
{
book.Title = collection["Title"].ToString();
book.ISBN = collection["ISBN"].ToString();
book.Price = Convert.ToDecimal(collection["Price"]);
_db.Books.Add(book);
_db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(book);
}
}
catch
{
return View();
}
}
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 13/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 15
It will add theEdit.cshtmlfile:
@model ASPNETMVCApplication.Models.Book
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2><script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"></script><script
src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript"></script>@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Book</legend>@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ISBN)
</div>
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 14/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
<div class="editor-field">
@Html.EditorFor(model =>
return View(book);
}
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
var book = _db.Books.Find(id);
UpdateModel(book);
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
ModelState.AddModelError("", "Edit Failure, see inner exception");
return View();
}
}
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 15/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 16
It will add theDetails.cshtmlfile:
@model ASPNETMVCApplication.Models.Book
@{
ViewBag.Title = "Details";
}
<h2>Details</h2><fieldset>
<legend>Book</legend><div class="display-label">Title</div>
<div class="display-field">@Model.Title</div>
<div class="display-label">ISBN</div>
<div class="display-field">@Model.ISBN</div>
<div class="display-label">Price</div>
<div class="display-field">@String.Format("{0:F}", Model.Price)</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 16/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
if (book == null)
return RedirectToAction("Index");
Figure 17
It will add theDelete.cshtmlfile.
@model ASPNETMVCApplication.Models.Book
@{
ViewBag.Title = "Delete";
}
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 17/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
<div class="display-label">ISBN</div>
<div class="display-field">@Model.ISBN</div>
<div class="display-label">Price</div>
<div class="display-field">@String.Format("{0:F}", Model.Price)</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
return View(book);
}
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
Book book = _db.Books.Find(id);
_db.Books.Remove(book);
_db.SaveChanges();
return RedirectToAction("Index");
}
Now run the application. Type/Bookin the URL and remember that the port
number may be different in your case.
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 18/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 17
ClickCreate New Linkto add a new book.
Figure 18
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 19/20
8/7/2017 Basic operations Insert, Update and Delete of Data from SQL Server in ASP.NET MVC | eRevolution
Figure 19
This way you can insert, Update, delete, and view details of the books.
http://www.xtendedrevolution.co.in/2013/07/basic-operations-insert-update-and.html 20/20