title: How do you call a stored procedure in MVC3 C#
body: <p>I am looking for a way to call my stored procedure in my C# code in the controller. I already have everything that I need in the controller. I have the ID on the post of the edit, and what I need to do when submitted is call the stored procedure with the parameter id. 
  In SQL you do this:   </p>
  
  <pre><code> EXEC [dbo].[AddGlassCutting] @AuditScheduleID = 1192
  </code></pre>
  
  <p>to test. This does not work in my code for the site. 
  The database is done with code first approach.</p>
  
  <p>Updated 1:33 Sept 14 2012</p>
  
  <p>Added code from edit Post - this is where i need it too execute:</p>
  
  <pre><code>        [HttpPost]
      public ActionResult Edit(int id, AuditScheduleEdit viewModel)
      {            
          if (ModelState.IsValid)
          {
              viewModel.PopulateCheckBoxsToSpacerType();
              _db.Entry(viewModel.AuditScheduleInstance).State = System.Data.EntityState.Modified;
              _db.SaveChanges();
              return RedirectToAction("Audit", new {id});
          }
          else
          {
              return View(viewModel);
          }
  
      }
  </code></pre>
  
  <p>Updated 09/18/2012 </p>
  
  <pre><code>        [HttpPost]
      public ActionResult Edit(int id, AuditScheduleEdit viewModel)
      {            
          if (ModelState.IsValid)
          {
              var addGlassCutting = new SqlParameter("@AuditScheduleID", id);
              _db.Database.SqlQuery&lt;QQAForm.ViewModels.AuditScheduleEdit.AddGlassCutting&gt;
                  ("AddGlassCutting @AuditScheduleID", addGlassCutting);
              viewModel.PopulateCheckBoxsToSpacerType();
              _db.Entry(viewModel.AuditScheduleInstance).State = System.Data.EntityState.Modified;
              _db.SaveChanges();
              return RedirectToAction("Audit", new {id});
          }
          else
          {
              return View(viewModel);
          }
  
      }
  </code></pre>
  
  <p>Here is my Stored Procedure: </p>
  
  <pre><code>ALTER PROCEDURE [dbo].[AddGlassCutting] 
  -- Add the parameters for the stored procedure here
  @AuditScheduleID int = null
  AS
  BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;
  
  -- Insert statements for procedure here
  /****** Script for adding Glass Cutting  ******/
  INSERT INTO QQAEntities.dbo.MainAnswers (AuditScheduleID, MainQuestionID)
  SELECT @AuditScheduleID, MainQuestionID
  FROM QQAEntities.dbo.MainQuestions
  WHERE ReferenceNo &gt; 0
  </code></pre>
  
  <p>END</p>
  
  <p>At this point there are no errors in the code, it compiles, but when it is executed it does not run the stored procedure. </p>
  
  <p>Any Help would be appreciated. </p>