Marieee

Marieee

  • NA
  • 12
  • 2.6k

Select multiple values from listbox and insert in SQL db

Feb 25 2017 1:44 PM
Hi!
I'm working on an ASP.NET website in C#. The idea is to insert data into a SQL Server database through the website. I wrote a stored procedure for insert data and code in C#. The data from Table 3 is inserting through Listbox, but as a string, not as foreign key. I think that is a bad way because of searching data.
Does anyone have an idea hot to insert ID from Table 3 in Table 1, through Listbox, if I select multiple text values from that table?
Fields in Table 3: ID, col5.
This is stored procedure.
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[TEST]
// Table1
@col1 NVARCHAR(MAX),
@col2 NVARCHAR(MAX),
@col3 NVARCHAR(MAX),
@col4 NVARCHAR(MAX),
// Table 2
@FileName NVARCHAR(50),
@FilePath NVARCHAR(200),
// Table 3
 @col4 NVARCHAR(MAX)
 
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Table_Var TABLE (ID1 INT)
INSERT INTO Table1 (col1,col2,col3,col4,COL5)
OUTPUT inserted.ID1
INTO @Table_Var(ID1)
SELECT @col1,@col2,@col3,@col4, @COL5
INSERT INTO Table2 (colID,FileName,FilePath)
SELECT ID1,@FileName,@FilePath
FROM @Table_Var
END
 
This is part of code that is inserting text value from Table 3, through Listbox and Label.
 
foreach (ListItem item in Table3.Items)
{
if (item.Selected)
{
try
{
Label1.Text = item.Text + ", " + Label1.Text;
cmd.Parameters["@COL5"].Value = Label1.Text;
}
catch (Exception ex)
{
}
}
}
 
 
 
 

Answers (1)