I am trying to call a stored procedure from C# code and return a list of data. The stored procedure works well in SQL. When I run the code in C# I get the count of records but all the data is null. My current result is 192 rows of data when running stored procedure through SQL. When I debug in C# i see the 192 rows but they are all filled with null data. What do I need to do?
Stored Procedure
SELECT DISTINCT CA.Item
, CL.Chains
, S.Sub_Name
, I.Description AS 'Description'
, I.SizeDesc AS 'Size'
, Left(I.UpcRetail, LEN(I.UpcRetail)-1) AS 'UPC'
, CL.AssignedToUserId
FROM ChainAuth_ChainAuth CA
INNER JOIN ChainAuth_ChainList CL
ON CA.ChainId = CL.ChainId
INNER JOIN HeidDWHSE.dbo.ItemV I
ON CA.[Item] = cast(I.Item as int)
INNER JOIN HeidDWHSE..Sub S
ON I.Sub = S.Sub
WHERE CA.ChainId = @ChainID
END
C# 1st Attempt
public List<ChainAuthorizations> GetAuthListByChain(string chainId)
{
var parameters = new[] {
new SqlParameter("@ChainId", chainId)
};
var auths = hc.Database.SqlQuery<ChainAuthorizations>("EXEC
usp_GetAuthorizedItemDetails @ChainId",
parameters).ToList<ChainAuthorizations>();
using (SqlCommand command = new SqlCommand("usp_GetAuthorizedItemDetails"))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@ChainId", chainId));
conn.Open();
command.Connection = conn;
using (SqlDataReader reader =
command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
int item = reader.GetInt32(reader.GetOrdinal("Item"));
string chains = reader.GetString(reader.GetOrdinal("Chains"));
string sub_Name = reader.GetString(reader.GetOrdinal("Sub_Name"));
string description = reader.GetString(reader.GetOrdinal("Description"));
string size = reader.GetString(reader.GetOrdinal("Size"));
string upc = reader.GetString(reader.GetOrdinal("UPC"));
int assignedToUserId =
reader.GetInt32(reader.GetOrdinal("AssignedToUserId"));
}
}
}
}
return auths;
}
C# 2nd attempt
public List<ChainAuthorizations> GetAuthListByChain(string chainId)
{
List<ChainAuthorizations> cAuths = new List<ChainAuthorizations>();
SqlConnection con = new SqlConnection("connection string data here");
SqlDataAdapter sda = new SqlDataAdapter("usp_GetAuthorizedItemDetails",
con);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add(new SqlParameter("@chainId",
SqlDbType.VarChar));
sda.SelectCommand.Parameters["@chainId"].Value = chainId;
DataTable dt = new DataTable();
sda.Fill(dt);
cAuths = dt.ToString();
return cAuths.ToList();
}
CommandBehavior.CloseConnectionfrom first attempt and check is therereader.HasRowsistrueorfalse