Solved

Stored Procedure using parameters from listbox - multiple select

Posted on 2014-09-15
2
205 Views
Last Modified: 2014-10-14
Hi have 2 listboxs with selectmode set to 'multiple', the options in the listboxs are:

All
1
2
3

All
a
b
c

The comma separated list I'm creating to pass to my sql looks like this:

           
String strSegment = String.Empty;
            foreach (ListItem li in lstNum.Items)
            {
                if (liSegment.Selected)
                {
                    strSegment += "'" + liSegment.Value + "',";
                }
            }
            strSegment = strSegment.Substring(0, strSegment.Length - 1);

Open in new window


My Stored Procedure looks like this:

      
select * from 
	VIEW_TOTAL
	where
	Numbern in (@NumList)
	and 
	Letters in (@LetterList)

Open in new window

     
I'm lost about what to do when a user select 'All' or 'All','1','2','3' or doesn't select anything.
I know these are a lot of questions, I'm hoping to get some guidance or an example of how to handle passing multi select values to sql to return a dataset.  Thank you in advance for any assistance.
0
Comment
Question by:Scarlett72
2 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 40323449
You can set the value for All to 1,2,3 and pass it to the storedproc.
And when nothing is selected you pass null.
Check on that null in the storedproc and take appropriate action.
0
 
LVL 1

Accepted Solution

by:
lmred earned 500 total points
ID: 40332635
You can do something like this to get you started:

@String NVARCHAR(40)
@String2 NVARCHAR(40)

DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @Delimiter NVARCHAR(40)

IF (@string IS NULL)
Begin
SET @Delimiter = ','
--SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)
END
End

IF (@string2 IS NOT NULL)
Begin
--SET @String2 = @String2 + @Delimiter
SET @Pos = charindex(@Delimiter,@String2)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String2,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String2,@pos+1,len(@String2))
SET @pos = charindex(@Delimiter,@String2)
END
End
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question