Solved

Stored Procedure using parameters from listbox - multiple select

Posted on 2014-09-15
2
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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