Solved

pass value of listbox items to select parameter

Posted on 2014-09-15
2
852 Views
Last Modified: 2014-10-14
I have a form with a multi list box:

<asp:ListBox ID="lstAction" runat="server" Rows="3" SelectionMode="Multiple"></asp:ListBox>

Open in new window


I have a stored procedure that's getting data from an objectdatasource, when one value is passed it works ok

 
      <asp:ObjectDataSource ID="odsSalesSC" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="RapidFire.RFTableAdapters.USP_TOTAL_ORDERCALLSTableAdapter" OnSelecting="odsSalesSC_Selecting">
            <SelectParameters>
                <asp:ControlParameter ControlID="lstAction" Name="action" PropertyName="SelectedValue" Type="String" ConvertEmptyStringToNull="true" />                
            </SelectParameters>
        </asp:ObjectDataSource>

Open in new window


But when a user select mulitple options I have build a string from the  selection that pass into a storedprocedure like where something in ('a','b','c').
How can I get the values from strAction to pass into my select parameter?

protected void btnFilter_Click(object sender, EventArgs e)
        {            
            String strAction = String.Empty;           
            foreach (ListItem liAction in lstAction.Items)
            {
                if (liAction.Selected)
                {
                    strAction += "'" + liAction.Value + "',";
                }
            }
            strAction = strAction.Substring(0, strAction.Length - 1);
}

Open in new window

0
Comment
Question by:Scarlett72
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40324702
123,456,789,101112,13141516,1718192021,222324252627

that APPEARS to be array of values, so why doesn't SQL just understand that? Well, is it really an array to start with?

NO! As you generate the sql statement to send, it becomes text, or a "string" so to SQL it looks like this:

'123,456,789,101112,13141516,1718192021,222324252627'

It is just a single string - that just happens to have many digits and commas in it, to SQL it's the same type of data as this:

'Lorem Ipsum is simply dummy text of the printing and typesetting industry  since the 1500s'

On top of which SQL doesn't work with arrays anyway.

I'm not a .NET or C# person, but try these articles to see if they help:
Delimited list as parameter: what are the options? by angeliii (SQL)
Passing lists and complex data through a parameter by aikimark (VB)

{+edit}
here are 2 previously answered questions on the topic with .NET
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28329379.html#accepted-solution
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28329183.html#accepted-solution

both refer to a function: dbo.ParmsTolist()
see:
http://www.experts-exchange.com/Database/MS-SQL-Server/Q_21627393.html#discussion
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40325218
I am sure you understand your code is no where near complete.

Since you will be calling a stored proc, as I understand your question, then you would need something like this:

SqlCommand cmd = new SqlCommand("storedprocName", Conn);
cmd.CommandType = CommandType.StoredProcedure;

//Then pass select as parameter:

cmd.Parameters.AddWithValue("@lstAction", strAction);

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

23 Experts available now in Live!

Get 1:1 Help Now