Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hey friends,
please help me to solve a problem.

how to get Id of all List item placed in Right List Box and how to pass all these Id in store procedure as a parameter, where my query is like this

select * from tablename where Id In(listitems)

thanks in advance.
Posted

Hi,

Its very simple.... you can use the User Define Data type for this. check the below links....
How to pass multiple records to a Stored Procedure[^]
Table Value Parameter in SQL Server 2008[^]
Regards,
GVPrabu
 
Share this answer
 
Comments
Maciej Los 24-Sep-13 8:46am    
Gopal, i think OP wants to know how to collect values from ListBox and how to pass it as a string to SP.
gvprabu 24-Sep-13 8:53am    
yes... from back end part we can use UDT in SP right.... In front end he need to make code....
Fisrt step: Get the items from listBox (VB):

VB
(...)
Dim csv As New System.Text.StringBuilder()
Dim first As Boolean = True

For Each item As String In Me.ListBox1.SelectedItems
    csv.Append(If(first, String.Empty, ",") + item)
    first = False
Next

(...)
cmd.Parameters.AddWithValue("@paramName", csv.ToString())
(...)


This passes "item1,item2,arreloko" to the SP.

Add the following help function to your DB:

CREATE function [dbo].[fn_ParseCSVString]
(
@CSVString 	varchar(8000) ,
@Delimiter	varchar(10)
)
returns @tbl table (s varchar(1000))
as

begin
declare @i int ,
	@j int
	select 	@i = 1
	while @i <= len(@CSVString)
	begin
		select	@j = charindex(@Delimiter, @CSVString, @i)
		if @j = 0
		begin
			select	@j = len(@CSVString) + 1
		end
		insert	@tbl select substring(@CSVString, @i, @j - @i)
		select	@i = @j + len(@Delimiter)
	end
	return
end


Then use this in your query:

SQL
--using on WHERE
SELECT * FROM YourTable WHERE column1 IN (select*from dbo.fn_ParseCSVString(@paramName, ','))

--using on joins
SELECT * FROM YourTable t JOIN dbo.fn_ParseCSVString(@paramName, ',') csv on t.column1 = csv.s
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900