Database code

create database mvc

create table tbl_products
(
id int primary key identity(1,1),
product_name varchar(100),
quantity int
)

insert into tbl_products values ('rice',100)

create procedure get_products
as begin
select * from tbl_products
end
go
create procedure insert_products
(
@product_name varchar(100),
@quantity int
)
as begin
insert into tbl_products values
(@product_name,@quantity)
end
insert_products 'dhall',50

edit_products 1,'salt',50
create procedure edit_products
(
@id int,
@product_name varchar(100),
@quantity int
)
as begin
update tbl_products set product_name=@product_name
, quantity=@quantity where id =@id
end

create procedure delete_products
(
@id int
)
as begin
delete from tbl_products where id =@id
end

go

create procedure get_products_id
(@id int
)
as begin
select * from tbl_products where id=@id
end

Model Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ArunDepartmentalStore.Models
{
public class tbl_products
{
public int id { get; set; }
public string product_name { get; set; }
public int quantity { get; set; }

}
}

Controller Code

using ArunDepartmentalStore.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ArunDepartmentalStore.Controllers
{
public class tbl_productsController : Controller
{
string constr = "Data Source=.\\sqlexpress;Initial Catalog=mvc;Integrated Security =true";

// GET: tbl_products
public ActionResult Index()
{
List<tbl_products> tbl_products_obj = new List<tbl_products>();
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("get_products", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while(sdr.Read())
{
tbl_products_obj.Add(new tbl_products
{
id = Convert.ToInt32( sdr["id"]),
product_name = Convert.ToString(sdr["product_name"]),
quantity = Convert.ToInt32(sdr["quantity"])

}
);
}
con.Close();
return View(tbl_products_obj);
}

// GET: tbl_products/Details/5
public ActionResult Details(int id,tbl_products tbl_products_obj)
{
SqlConnection con = new SqlConnection(constr);
string query = "get_products_id " + id;
SqlCommand cmd = new SqlCommand(query, con);

con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
tbl_products_obj = new tbl_products
{
id = Convert.ToInt32(sdr["id"]),
product_name = Convert.ToString(sdr["product_name"]),
quantity = Convert.ToInt32(sdr["quantity"])

};
}
con.Close();
return View(tbl_products_obj);
}

// GET: tbl_products/Create
public ActionResult Create()
{
return View();
}

// POST: tbl_products/Create
[HttpPost]
public ActionResult Create(tbl_products tbl_productobj)
{
try
{
SqlConnection con = new SqlConnection(constr);
string query = "insert_products '" + tbl_productobj.product_name + "'," + tbl_productobj.quantity;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

// GET: tbl_products/Edit/5
public ActionResult Edit(int id)
{
return View();
}

// POST: tbl_products/Edit/5
[HttpPost]
public ActionResult Edit(int id, tbl_products tbl_productobj)
{
try
{
SqlConnection con = new SqlConnection(constr);
string query = "edit_products " + id +",'" + tbl_productobj.product_name + "'," + tbl_productobj.quantity;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

// GET: tbl_products/Delete/5
public ActionResult Delete(int id,tbl_products tbl_products_obj)
{
SqlConnection con = new SqlConnection(constr);
string query = "get_products_id " + id;
SqlCommand cmd = new SqlCommand(query, con);

con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
tbl_products_obj = new tbl_products
{
id = Convert.ToInt32(sdr["id"]),
product_name = Convert.ToString(sdr["product_name"]),
quantity = Convert.ToInt32(sdr["quantity"])

} ;
}
con.Close();
return View(tbl_products_obj);

}

// POST: tbl_products/Delete/5
[HttpPost]
public ActionResult Delete(int id)
{
try
{
SqlConnection con = new SqlConnection(constr);
string query = "delete_products " + id ;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");

}
catch
{
return View();
}
}
}
}

View Code

Index.cshtml

Index.cshtml
@model IEnumerable<ArunDepartmentalStore.Models.tbl_products>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.product_name)
</th>
<th>
@Html.DisplayNameFor(model => model.quantity)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.product_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.quantity)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}

</table>

create.cshtml

create.cshtml
@model ArunDepartmentalStore.Models.tbl_products

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>tbl_products</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.product_name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.product_name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.product_name, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.quantity, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

edit.cshtml

edit.cshtml
@model ArunDepartmentalStore.Models.tbl_products

@{
ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>tbl_products</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)

<div class="form-group">
@Html.LabelFor(model => model.product_name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.product_name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.product_name, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.quantity, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

Details.cshtml

Details.cshtml
@model ArunDepartmentalStore.Models.tbl_products

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
<h4>tbl_products</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.product_name)
</dt>

<dd>
@Html.DisplayFor(model => model.product_name)
</dd>

<dt>
@Html.DisplayNameFor(model => model.quantity)
</dt>

<dd>
@Html.DisplayFor(model => model.quantity)
</dd>

</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.id }) |
@Html.ActionLink("Back to List", "Index")
</p>

Delete.cshtml

Delete.cshtml
@model ArunDepartmentalStore.Models.tbl_products

@{
ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
<h4>tbl_products</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.product_name)
</dt>

<dd>
@Html.DisplayFor(model => model.product_name)
</dd>

<dt>
@Html.DisplayNameFor(model => model.quantity)
</dt>

<dd>
@Html.DisplayFor(model => model.quantity)
</dd>

</dl>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()

<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>

Categorized in: