Solved

pass value of listbox items to select parameter

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

734 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