0

I have app running in MVC and I am intended to send data from MVC app C# to Store Procedure. I have tested my stored procedure, it works on if I execute from SQL Server Management Studio, and in C# I am getting all the values from web page (view) to function where it is expecting to call store procedure so I am guessing I am doing something wrong in parameter passing!!!

Its not storing data in database

Many thanks in advance

Store Procedure

USE [MySolution01_DB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
 @FunctionName nvarchar(250),
 @Hierarchy_Level INT,
 @Function_identity INT OUTPUT, 
 @ControllerName nvarchar(250), 
 @Controller_identity INT OUTPUT,
 @ControllerInFunction_identity INT OUTPUT,
 @ActionName nvarchar(250),
 @Action_identity INT OUTPUT,
 @ActionInFunction_identity INT OUTPUT,
 @Function_ParentsFunctionID INT,
 @Function_ParentsFunction_identity INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;

INSERT INTO [dbo].[Navigation_Functions] ([FunctionName],[Hierarchy_Level] ) 
VALUES(@FunctionName, @Hierarchy_Level)
SET @Function_identity=SCOPE_IDENTITY()


INSERT INTO [dbo].[Navigation_FunctionController] ([ControllerName])
VALUES(@ControllerName)
SET @Controller_identity = SCOPE_IDENTITY()


INSERT INTO [dbo].[Navigation_FunctionInController] ([Function_ID], [ControllerID])
VALUES (@Function_identity, @Controller_identity)
SET @ControllerInFunction_identity = SCOPE_IDENTITY()

INSERT INTO [dbo].[Navigation_FunctionAction] ([ActionName], [ControllerID])
VALUES (@ActionName, @Controller_identity)
SET @Action_identity = SCOPE_IDENTITY()

INSERT INTO [dbo].[Navigation_FunctionInAction] ([ActionID], [Function_ID])
VALUES (@Action_identity, @Function_identity)
SET @ActionInFunction_identity = SCOPE_IDENTITY()

INSERT INTO [dbo].[Navigation_FunctionHierarchy] ([Function_IDs], [Parent_Function_ID])
VALUES (@Function_identity, @Function_ParentsFunctionID)
SET @Function_ParentsFunction_identity = SCOPE_IDENTITY()

RETURN
END

Model Class

  public class CreateFunctionNavigation_SP_Map
{
    public CreateFunctionNavigation_SP_Map()
    {

    }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Function Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Function Hierarchy; i.e Where Function Exists In Hierarchy Tree \n Top-Level Start From 1 ")]
    [Display(Name = "Function Hierarchy Level")]
    public int FunctionHierarchy_Level { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Controller Title")]
    [Display(Name = "Controller Title")]
    public string ControllerName { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Action Title")]
    [Display(Name = "Action Title")]
    public string ActionName { get; set; }

    [Required(ErrorMessage = "Required Function Parent - Child Relation ID \n Put 0 In Case Given Function doesn't Have Any Parent Function ")]
    [Display(Name = "Function Parent's FunctionID")]
    public int Function_ParentsFunctionID { get; set; }    

}

Class to call Stored Procedure

public void CreateFunctionNavigation(CreateFunctionNavigation_SP_Map _entity)
    {

        using(var dbContext = new FunctionContext())
        {
            CreateFunctionNavigation_SP_Map modelObj = new CreateFunctionNavigation_SP_Map();

            var parameters = new[] { 
                new SqlParameter("@FunctionName" , _entity.FunctionName ),
                new SqlParameter("@FunctionHierarchy_Level" , _entity.FunctionHierarchy_Level ),
                new SqlParameter("@ControllerName" , _entity.ControllerName ),
                new SqlParameter("@ActionName" , _entity.ActionName ),
                new SqlParameter("@Function_ParentsFunctionID" , _entity.Function_ParentsFunctionID )
            };


             dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateNewFunctionsNavigation", parameters);
        }


    }

new update

I have change C# code but still not able to store data

dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("EXEC CreateNewFunctionsNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID",
       new SqlParameter("FunctionName", _entity.FunctionName),
       new SqlParameter("FunctionHierarchy_Level", _entity.FunctionHierarchy_Level),
       new SqlParameter("ControllerName", _entity.ControllerName),
       new SqlParameter("ActionName", _entity.ActionName),
       new SqlParameter("Function_ParentsFunctionID", _entity.Function_ParentsFunctionID)
   );
K.Z
  • 5,201
  • 25
  • 104
  • 240
  • possible duplicate of [How to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5](http://stackoverflow.com/questions/4873607/how-to-use-dbcontext-database-sqlquerytelementsql-params-with-stored-proced) – tschmit007 Jan 13 '15 at 17:12

1 Answers1

0

You could have debugged this by using SQL Profiler which shows commands executed against the server. Anyways, try the following

dbContext.Database.SqlQuery("CreateNewFunctionsNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID", parameters);

Update

Since the answer above didnt work for you, you could try and create the query string manually like this..

        var query = string.Format("EXEC CreateNewFunctionsNavigation " +
                                  "@FunctionName = {0}, " +
                                  "@FunctionHierarchy_Level = {1}, " +
                                  "@ControllerName = {2}, " +
                                  "@ActionName = {3}, " +
                                  "@Function_ParentsFunctionID = {4}",
                                  _entity.FunctionName,
                                  _entity.FunctionHierarchy_Level, 
                                  entity.ControllerName,
                                  _entity.ActionName,
                                  _entity.Function_ParentsFunctionID);


        dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>(query);
heymega
  • 9,215
  • 8
  • 42
  • 61
  • i have did as above in last session of my question but no data entry .... I am using SQL Server Management Studio 2012 and I dont have SQL Profiler ??? can I donwload that! – K.Z Jan 13 '15 at 20:30
  • Go to tools>SQl Server Profiler. If its not there then you probably didnt install the management tools when you installed MSSQL. If you run the installation again you can add this feature on. With regards to question, I'll update my answer with another method you can try – heymega Jan 14 '15 at 09:21