This
article will show you how you can insert or save data into database by stored
procedure using entity framework in asp.net mvc using c#.net. In this we will
be save data be store procedure using c#.net.
So for this article first we will anew table and add some record.
So for this article first we will anew table and add some record.
After
adding image we will create an stored procedure.
CREATE PROCEDURE
[dbo].[SP_SaveStudentDetail]
@Name varchar(50),
@Address varchar(50),
@Section varchar(50),
@Marks int
AS
BEGIN
INSERT INTO
[dbo].[StudentDetail]
([Name]
,[Address]
,[Section]
,[Marks])
VALUES
(@Name,
@Address,
@Section,
@Marks)
select Cast(@@Identity as Int);
END
|
Now we will
create a new asp.net MVC application and add an entity file and add the created
stored procedure. For adding stored procedure we will right click on entity
file and click on Update model From DataBase.
Here we
need to do one thing we need to uncheck the below shown option. This we will
are doing because I want to show you the process in detail for creating the
function for the stored procedure.
Now right
click and select the Add Function Import…
Now we need
to make some small configuration. Here you have to provide the SP function name
and the scalars type. In this we will return we will select int type. After this in model browser section you will get the
below screen.
Now we will
add model class file. Now add the below code in model class file.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC_Demos.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Section { get; set; }
public string Marks { get; set; }
}
}
|
After
adding this we will add controller class file and add the below code.
using MVC_Demos.Models;
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MVC_Demos.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
Student _studentModel = new Student();
return View(_studentModel);
}
[HttpPost]
public ActionResult
Index(Student
_studentModel)
{
try
{
DemoEntities
_demoEntities = new DemoEntities();
_demoEntities.SP_SaveStudentDetail(_studentModel.Name,
_studentModel.Address, _studentModel.Section, Convert.ToInt32(_studentModel.Marks)).ToList();
ViewBag.Message = "Data
saved successfully.";
}
catch
{
ViewBag.Message = "Error
while saving record.";
}
return
View(_studentModel);
}
}
}
|
In above code please check the highlighted part of the code. In above code I have created the instance of the entity class. After this we will use entity object to access the created function of the Stored Procedure.
@model MVC_Demos.Models.Student
@{
ViewBag.Title
= "Insert or Save Data Into Database
By Stored Procedure Using Entity Framework In Asp.net MVC";
}
@using (Html.BeginForm("Index", "Home"))
{
<table width="100%" cellpadding="5" cellspacing="2" border="0" style="background-color: White;" class="gridtable">
<tr>
<td colspan="2"><b>@ViewBag.Message</b></td>
</tr>
<tr>
<td>Name</td>
<td>@Html.TextBoxFor(m => m.Name)</td>
</tr>
<tr>
<td>Marks</td>
<td>@Html.TextBoxFor(m => m.Marks)</td>
</tr>
<tr>
<td>Address</td>
<td>@Html.TextBoxFor(m => m.Address)</td>
</tr>
<tr>
<td>Section</td>
<td>@Html.TextBoxFor(m => m.Section)</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save" />
</td>
</tr>
</table>
}
|
Now run the
application to check the output.
Now we will
check the SQL table for the data.
0 comments:
Please let me know your view