Solved

pass value of listbox items to select parameter

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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
Web Form VB.Net  import CSV 4 27
How to make a GridView cell hyperlinked using C# ? 3 18
MSSQL Speen Degradation 4 10
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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 …

895 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

12 Experts available now in Live!

Get 1:1 Help Now