Inserting record using Stored procedure

Total Post:52

Points:366
Mvc4 
C#
 1006  View(s)
Ratings:
Rate this:

I want to insert record using stored procedure,I am using entity framework

Here is my sample code.


please anyone help me 

 

 [HttpPost]
        public ActionResult Add(RegistrationForm Model)
        {
     using(var context = new DemoEntities())
            {
                if (Model.Id == 0)
                {
                    context.RegistrationForms.Add(Model);
                }
                else
                {
                    context.Entry(Model).State= EntityState.Modified;
                }
 
                // context.RegistrationForms.Add(Model);
                ViewData["Message"] = "Success";
 
            }
            Return View();
        }
  1. Post:19

    Points:135
    Re: Iserting record using Storeprocedure

    Create store procedure according to your form field

     

    Create Proc [dbo].[InsertRecord]
    (
    @Fname varchar(50),
    @MName varchar(50 ),
    @LName varchar(50),
    @Dob datetime,
    @Mobile nchar(10),
    @City varchar(50),
    @Pin varchar(50),
    @Address varchar(250)
    )
    As
    Begin
    Insert into RegistrationForm(FName,MName,LName,Dob,Mobile,City,Pin,Address) values(@Fname,@MName,@LName,@Dob,@Mobile,@City,@Pin,@Address)
    end
     
     
     
     
      [HttpPost]
     
            public ActionResult Add(RegistrationForm Model)
            {
                using (var context = new DemoEntities())
                {
                    if (Model.Id == 0)
                    {
                        //calling store procedure of InsertRecord
                        context.InsertRecord(Model.FName, Model.MName, Model.LName, Model.Dob,Model.Mobile, Model.City, Model.Pin, Model.Address);
                    }
                    else
                    {
                        context.UpdateRecord(Model.Id, Model.FName, Model.MName, Model.LName,Model.Dob, Model.Mobile, Model.City, Model.Pin, Model.Address);
                    }
     
                    // context.RegistrationForms.Add(Model);
                    ViewData["Message"] = "Success";
     
                }
                Return View();
     
            }

      Modified On Apr-11-2018 10:28:08 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!