Solved

pass value of listbox items to select parameter

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

735 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