I need Crud Operations Insert,Update,Delete EF MVC3

Posted by Jayakumars under ASP.NET on 7/10/2013 | Points: 10 | Views : 2515 | Status : [Member] [MVP] | Replies : 3
Hi

I need CRUD Operations With EntityFramework insert,Update,Delete in ASP.NET MVC 3

Step by step snapshots need . any one post this

Mark as Answer if its helpful to you


Responses

Posted by: Bandi on: 7/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer the following link for CRUD operations with MVC3 & EF
http://code.msdn.microsoft.com/Detail-CRUD-Operations-fbe935ef#content

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 7/30/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Initially, create a class (Products.cs) in your Models folder and write the following code...

private Freshers2012Entities db = new Freshers2012Entities();
public int ProductId { get; set; }
public string ProductName { get; set; }
public int ProductDiscount { get; set; }
public decimal MRP { get; set; }
public string TaxType { get; set; }
public decimal Tax { get; set; }
public int Discount { get; set; }

public bool SaveDetails(string ProductName, string ProductDiscount, string MRP, string TaxType, string Tax)
{
try
{
ProductsMvc objPdt = new ProductsMvc();
objPdt.ProductName = ProductName;
objPdt.ProductDiscount = Convert.ToInt32(ProductDiscount);
objPdt.MRP = Convert.ToDecimal(MRP);
objPdt.TaxType = TaxType;
objPdt.Tax = Convert.ToDecimal(Tax);
db.ProductsMvcs.AddObject(objPdt);
db.SaveChanges();
return true;
}
catch
{
return false;
}
}


private bool DeleteUser(int ProductId)
{
try
{
var query = (from d in db.ProductsMvcs
where d.ProductId == ProductId
select d).FirstOrDefault();
db.ProductsMvcs.DeleteObject(query);
db.SaveChanges();
return true;
}
catch
{
return false;
}
}

public bool UpdateUser(int productId, string productName, string productDiscount, string mRP, string taxType, string tax)
{
try
{
var query = (from d in db.ProductsMvcs
where d.ProductId == productId
select d).FirstOrDefault();
query.ProductId = productId;
query.ProductName = productName;
query.ProductDiscount = Convert.ToInt32(productDiscount);
query.MRP = Convert.ToDecimal(mRP);
query.TaxType = taxType;
query.Tax = Convert.ToDecimal(tax);
db.SaveChanges();
return true;
}
catch
{
return false;
}
}



Step 1:
Create an object for the Products class as below:
Products objPdtMdl = new Products();

Write an ActionMethod in Controller as below:
public ActionResult Products()
{
// display all the products that are present in your table using linq
var products = from p in db.ProductsMvcs
select new
{
ProductId = p.ProductId,
ProductName = p.ProductName,
ProductDiscount = p.ProductDiscount,
MRP = p.MRP,
TaxType = p.TaxType,
Tax = p.Tax
};
return View(products.ToList());
}

Here add you table (ProductsMvcs) using entity framework, one you add the table a connectionString will get generated in your web.Config file as follows,
<add name="Freshers2012Entities" connectionString="metadata=res://*/Models.Entities.csdl|res://*/Models.Entities.ssdl|res://*/Models.Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=PRIMEDB01\SQL2008;initial catalog=Freshers2012;user id=fresher;password=fresher@prime;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
Copy the name of the connnectionString and create an object for that connection string as below,
public class HomeController : Controller
{
Freshers2012Entities db = new Freshers2012Entities();

Step 2:
Create a View for the Action method (Products) and write the following code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
.gridTable
{
margin: 5px;
padding: 10px;
border: 1px #c8c8c8 solid;
border-collapse: collapse;
min-width: 550px;
background-color: #fff;
color: #fff;
}
.gridHead th
{
font-weight: bold;
background-color: #000000;
color: #fff;
padding: 10px;
}
.gridHead a:link, .gridHead a:visited, .gridHead a:active, .gridHead a:hover
{
color: #FFBBFF;
}
.gridHead a:hover
{
text-decoration: underline;
}
.gridTable tr.gridAltRow
{
background-color: #efeeef;
}
.gridTable tr:hover
{
background-color: #FFCCCC;
}
.gridAltRow td
{
padding: 10px;
margin: 5px;
color: #333;
}
.gridRow td
{
padding: 10px;
color: #333;
}
.gridFooter td
{
padding: 10px;
background-color: #c7d1d6;
color: #999;
font-size: 12pt;
text-align: center;
}
.gridFooter a
{
font-weight: bold;
color: #333;
border: 1px #333 solid;
}
.</style>
</head>
<body>
<script src="../../Scripts/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".add").live("click", function () {

var existrow = $('.save').length;
if (existrow == 0) {
var index = $("#DataTable tbody tr").length + 1;
var ProductId = "ProductId" + index;
var ProductName = "ProductName" + index;
var ProductDiscount = "ProductDiscount" + index;
var MRP = "MRP" + index;
var TaxType = "TaxType" + index;
var Tax = "Tax" + index;
var Save = "Save" + index;
var Cancel = "Cancel" + index;

var tr = '<

Kavya Shree Mandapalli

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 7/30/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

can you please Mark it as Answer.............

Regards,
Shree M.

Kavya Shree Mandapalli

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response