Solved

Stored Procedure using parameters from listbox - multiple select

Posted on 2014-09-15
2
201 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now