In advance, I appreciate there are lots of stored procedure related questions and answers out there on Stackoverflow, I know because over the last few days I have read a tonne of them but I still cannot get my head around the process of creating a stored procedure that will return values to my view model.
This question is in two parts:
- Why does my code not successfully run and instead returns -1 ?
- If with your help I get past that first issue how do I then get the returned data from the stored procedure into a list of my view model
I very much appreciate any help the community can give me...
- DB name is
Discovery
- Table name is
Person
- Stored procedure name is
uspGetOrgChart
My table
CREATE TABLE Discovery.dbo.Person
(
ADID nvarchar(50) NOT NULL,
First_Name nvarchar(50) NOT NULL,
Last_Name nvarchar(50) NOT NULL,
Report_To_ADID nvarchar(50) NULL,
Position_ID int NULL,
Role_ID int NULL,
IGEMS nvarchar(50) NULL,
DOB date NULL,
Start_Date date NULL,
Cost_Code nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (ADID),
CONSTRAINT FK_Person_Position1
FOREIGN KEY (Position_ID) REFERENCES dbo.Position (Position_ID),
CONSTRAINT FK_Person_Role
FOREIGN KEY (Role_ID) REFERENCES dbo.Role (Role_ID)
)
ON [PRIMARY]
GO
My stored procedure is currently as follows:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[uspGetOrgChart]
@ContactID VARCHAR(100) = 'NOE1WWD'
AS
BEGIN
SET NOCOUNT ON;
--grab id of @contactid
DECLARE @Test varchar(36)
SELECT @Test = (SELECT ADID FROM Person c1 WHERE c1.ADID = @ContactID)
;WITH StaffTree AS
(
SELECT
c.ADID,
c.First_Name,
c.Last_Name,
c.Report_To_ADID,
c.Report_To_ADID as Manager_ID,
cc.First_Name AS Manager_First_Name,
cc.Last_Name as Manager_Last_Name,
cc.First_name + ' ' + cc.Last_name AS [ReportsTo],
c.First_Name + ' ' + c.Last_Name as EmployeeName,
1 AS LevelOf
FROM
Person c
LEFT OUTER JOIN
Person cc ON c.Report_To_ADID = cc.ADID
WHERE
c.ADID = @Test
OR (@Test IS NULL AND c.Report_To_ADID IS NULL)
UNION ALL
SELECT
s.ADID,
s.First_Name,
s.Last_Name,
s.Report_To_ADID,
t.ADID,
t.First_Name,
t.Last_Name,
t.First_Name + ' ' + t.Last_Name,
s.First_Name + ' ' + s.Last_Name,
t.LevelOf + 1
FROM
StaffTree t
INNER JOIN
Person s ON t.ADID = s.Report_To_ADID
WHERE
s.Report_To_ADID = @Test
OR @Test IS NULL
OR t.LevelOf > 1
)
SELECT * FROM StaffTree
END
I have tested and the Procedure above works as I expected and returns the following columns:
ADID
First_Name
Last_Name
Report_To_ADID
Manager_ID
Manager_First_Name
Manager_Last_Name
ReportsTo
EmployeeName
LevelOf
My view model is called vmNewOrgChart
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Discovery.ViewModels
{
public class vmNewOrgChart
{
public string ADID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Report_To_Adid { get; set; }
public string Manager_ID { get; set; }
public string Manager_First_Name { get; set; }
public string Manager_Last_Name { get; set; }
public string ReportsTo { get; set; }
public string EmployeeName { get; set; }
public int LevelOf { get; set; }
}
}
This is where I start to get completely stuck
public IActionResult Orgchart(string id)
{
var personIdParam = new SqlParameter("@ContactID", SqlDbType.VarChar);
personIdParam.Value = id;
var result = _context.Database.ExecuteSqlCommand("exec uspGetOrgChart", personIdParam);
}
The code above runs but the value of result is -1 which means it returned nothing or some error?
Many thanks for any assistance
Derek