0

I have the following stored procedure:

CREATE PROCEDURE [dbo].[usp_GetReportLevel]
AS
    DECLARE @TEMP TABLE (BK_REPORT_LEVEL INT, REPORT_LEVEL_NAME VARCHAR(MAX))

    INSERT INTO @temp 
    VALUES (0, 'Client Group'), (1, 'Client'), (2, 'Related Matter'), (3, 'Matter')

    SELECT * 
    FROM @TEMP 
GO

The entity database like this:

    public IEnumerable<usp_GetReportLevel_Result> usp_GetReportLevel()
    {
        //const string storedProcedure = "usp_GetReportLevel";

        var result = Database.SqlQuery<usp_GetReportLevel_Result>($"EXEC usp_GetReportLevel");
        return result;
    }

The usp_GetReportLevel_Result as this:

public class usp_GetReportLevel_Result
{
    [Column("BK_REPORT_LEVEL")]
    public int BK_REPORT_LEVEL;
    [Column("REPORT_LEVEL_NAME")]
    public string REPORT_LEVEL_NAME;
}

and the c# code in a constructor to select the list items from the stored procedure as this section:

    List<SelectListItem> reportLevelList2= new List<SelectListItem>();

    var reportLevelusp = context.usp_GetReportLevel();

    foreach (var reportLevel in reportLevelusp)
    {
        SelectListItem lvl = new SelectListItem()
        {
            Text = reportLevel.REPORT_LEVEL_NAME,
            Value = reportLevel.BK_REPORT_LEVEL.ToString()
        };
        reportLevelList2.Add(lvl);
    }

    ReportLevelList = reportLevelList2;

Now if I debug that code, the foreach will enter 4 times, telling me that it is reading the database values right, but the values BK_REPORT_LEVEL and REPORT_LEVEL_NAME are always 0 and null respectively.

Why is this happening? The stored procedure is returning what I want to expect if I execute it on the database, Why Linq isn't getting the correct data? I can't see the error

enter image description here

10
  • 2
    Try adding SET NOCOUNT ON to the start of your procedure, does that help? Commented Jul 10, 2018 at 14:15
  • @DavidG no, still returning 0 and null Commented Jul 10, 2018 at 14:18
  • I assume you added it before the INSERT but after the AS? Commented Jul 10, 2018 at 14:19
  • I try it after the AS and after the DECLARE @TEMP TABLE, neither worked Commented Jul 10, 2018 at 14:21
  • 2
    Though this should really be in a table, not in a stored proc. Commented Jul 10, 2018 at 14:26

3 Answers 3

1

I have had issues with Selecting from Temp tables or table variables from SQL SP's before. Depends on what version of SQL Server you are dealing with. You need to use with result set:

https://www.mssqltips.com/sqlservertip/2356/overview-of-with-result-sets-feature-of-sql-server-2012/

Convert your call to use this:

EXEC usp_GetReportLevel
WITH RESULT SETS
(
 ( 
  BK_REPORT_LEVEL INT, 
  REPORT_LEVEL_NAME varchar(max)
 ) 
) 
Sign up to request clarification or add additional context in comments.

2 Comments

Assuming that you referred changing the entity database procedure to make a call like that, it is still returning the same values. If you mean changing anything else, I haven't try it yet.
also, this is the info of the sql server: Microsoft SQL Server Management Studio 14.0.17230.0
0

Try ExecuteSqlCommand:

  _context.Database.ExecuteSqlCommand("myproc");

Or with a return parameter:

 SqlParameter stmt = new SqlParameter("@stmt", System.Data.SqlDbType.Varchar);
 stmt.Direction = System.Data.ParameterDirection.Output;

 _context.Database.ExecuteSqlCommand("myproc @stmt out", stmt);
 var qry = stmt.value;

Make sure to put the out parameter in the procedure as well.

1 Comment

@Ragmar how about ToList(); like: var result = Database.SqlQuery<usp_GetReportLevel_Result>($"EXEC usp_GetReportLevel").ToList();
0

Didn't have to change anything, all was all right. I just needed to add { get; set; } on the variables....

public class usp_GetReportLevel_Result
{
    [Column("BK_REPORT_LEVEL")]
    public int BK_REPORT_LEVEL { get; set; }
    [Column("REPORT_LEVEL_NAME")]
    public string REPORT_LEVEL_NAME { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.