?
Solved

Stored Procedure using parameters from listbox - multiple select

Posted on 2014-09-15
2
Medium Priority
?
209 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

777 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