Monday, 7 August 2017

Insert or Save Data Into Database By Stored Procedure Using Entity Framework In Asp.net MVC

8/07/2017 - By Pranav Singh 0

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.



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.


Now below mention screen will open. In this we will select the created stored procedure.



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 we will open the Model Browser and select the stored procedure.

Now right click and select the Add Function Import…


After this below screen will open.


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.


Now we will create view for the action. And add the below code.

@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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top