I have created user defined data type and passing parameter ,how to pass that parameter in code using C# ,i'm facing issue while passing it is getting conversion failed from list to enumerable
eg:database server
ALTER procedure [dbo].[SpUpdateID]
@List AS dbo.STATUSID READONLY
AS
BEGIN
SET NOCOUNT ON;
Select ID from @List
END
CREATE TYPE [dbo].[STATUSID] AS TABLE(
[ID] [int] NULL
)
GO
code :
List
Tuhin PaulPosted Mar 15, 2023, 1:06 AM
Hello Meghana,
To pass a user-defined table type parameter to a stored procedure in C#, you need to create a DataTable object, fill it with the data that you want to pass, and then use it to create a SqlParameter object with SqlDbType.Structured. Here's an example of how you can modify your code to pass a list of integer IDs to the stored procedure:
We create a DataTable object idTable with a single column "ID" of type int, and populate it with the values from the idValues list using a foreach loop. We then create a SqlParameter object param with the name "@List", set its SqlDbType to SqlDbType.Structured, and its TypeName to "dbo.STATUSID" (the name of the user-defined table type). We execute the stored procedure using cmd.ExecuteReader() and read the results using a SqlDataReader object.