I want to use a SELECT
statement into a table based on multiple values from ListBox and get multiple IDs and then run an INSERT
statement and store the multiple IDs into a different table from INSERT
statement.
My code below is not working as I am getting "NULL" in single row instead of multiple IDs in multiple rows.
I am using a stored procedure for all the SQL statements.
Please see my code below:
Code-behind of my ASPX web page:
string listboxvalue = "";
foreach (ListItem item in listbox.Items)
{
if (item.Selected)
{
listboxvalue += item.Text + ',';
}
}
listboxvalue = listboxvalue.Substring(0, listboxvalue.Length - 1);
cmd.Parameters.AddWithValue("spselectvalue", listboxvalue);
Stored procedure:
@spselectvalue nvarchar(MAX),
// Select multiple Ids based on multiple items from list box
DECLARE @Dis TABLE (DisID int)
INSERT INTO @Dis
SELECT DId
FROM [table name]
WHERE [COLUMN] IN ('+@spselectvalue +')
EXEC sp_executesql @Dis
// Insert multiple Ids (from above select statement) into different table
INSERT INTO [dbo].[DifferentTable] ([SelectedIds])
VALUES
(
(SELECT DisID from @Dis)
)