Hi All,
I want to seek any solution for passing variable from .Net Core to Stored Procedure.
I'm using VS 2017 .net Core and my code base on this link. I'm workin on 2nd DropDownList (Sub Category) that dependent on FirstDropDownList (Category). I try to give breakpoint to check passing value inCategoryID and it works well, giving CategoryID value.
public JsonResult GetSubCategory(int CategoryID) { var Id_Category = new SqlParameter ("@Id_Category", CategoryID); List<Category_Sub> lst_Sub_Category = _project.Category_Sub.FromSql ("[dbo].[Select_Category_Sub] @Id_Category" , parameters: new [] { Id_Category.ToString()}).ToList ( ); return Json (new SelectList(lst_Sub_Category, "SubCategoryID", "SubCategoryName")); }
In this code above, I get error message:
Exception thrown: 'System.Data.SqlClient.SqlException' in Microsoft.EntityFrameworkCore.dll An exception of type 'System.Data.SqlClient.SqlException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user codeMust declare the scalar variable "@Id_Category".
and when I try to change the second parameter for "FromSQL" with
_project.Category_Sub.FromSql ("[dbo].[Select_Category_Sub] @Id_Category", Id_Category.ToString()).ToList ( );
I still got same error message. Even, I have changed the second parameter for "FromSQL" with
_project.Category_Sub.FromSql ("[dbo].[Select_Category_Sub] @Id_Category", CategoryID).ToList ( );
I still got error.
This is the Stored Procedure, I called.
ALTER PROCEDURE [dbo].[Select_Category_Sub] @Id_Category int AS BEGIN SET NOCOUNT ON; -- Insert statements for procedure here SELECT [SubCategoryID] , [SubCategoryName] FROM [dbo].[SubCategory] WHERE CategoryID = @Id_Category END
I refer to this link for calling Stored Procedure form "FromSQL"
As long as I know, in SQL Server, Must declare the scalar variable is just DECLARE & SET @asfdfas= ..... but this is in VS 2017, I have no idea ???
please, advice me...
Thank in Adv,
D