Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pass value of listbox items to select parameter

Posted on 2014-09-15
2
Medium Priority
?
985 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
[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 49

Accepted Solution

by:
PortletPaul earned 2000 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 29

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

721 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