Solved

Stored Procedure using parameters from listbox - multiple select

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 66
System.Speech 2 13
SYNTAX PROBLEM -- adding another column into the stored procedure output 30 32
Gridview alignment 1 9
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard 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.
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

24 Experts available now in Live!

Get 1:1 Help Now