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:
EXEC [dbo].[AddGlassCutting] @AuditScheduleID = 1192
to test. This does not work in my code for the site. The database is done with code first approach.
Updated 1:33 Sept 14 2012
Added code from edit Post - this is where i need it too execute:
[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);
}
}
Updated 09/18/2012
[HttpPost]
public ActionResult Edit(int id, AuditScheduleEdit viewModel)
{
if (ModelState.IsValid)
{
var addGlassCutting = new SqlParameter("@AuditScheduleID", id);
_db.Database.SqlQuery<QQAForm.ViewModels.AuditScheduleEdit.AddGlassCutting>
("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);
}
}
Here is my Stored Procedure:
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 > 0
END
At this point there are no errors in the code, it compiles, but when it is executed it does not run the stored procedure.
Any Help would be appreciated.