How do I pass all selected listbox item values into paramaterised query in ASP.NET c# (web application)?

Please can anyone tell me how to pass all selected listbox items into paramaterised SQL server query.

For example if I have a listbox containing the following items [apples, pears, bananas, grapes, oranges] and apple, pears and bananas have been selected by the user onclick. How do I pass apple, pear and bananas as a parameter into sql server paramaterised query using c# so that sql can be dynamially created and retrieve data into datagrid based on users selected values

And then how would I build up as I have several multiple listboxes.

I've tried googling for examples but no luck...I know how to get values from on selected value say if its from dropdown list but dont uderstand how to do for multi select listboxes.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

For getting the selected items, you would have to iterate through all items in the list and check the Selected property of each item.

Once you can get the selected items, You can build the Command object by passing Command parameters.

You can write an method which takes variable number of parameters as items and create sql parameters for each of the items passed and attach them to command object

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David RobitailleAnalyst ProgrammerCommented:
in the SQL use :
where field IN (SELECT     Value
                            FROM          dbo.ParmsToList(@Values, ',') AS ParmsToList_1))
in the BLL
to retrive the values, you could use something using 

Here ya go...use the sqldataadaptor...


using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication9
    public partial class Form1 : Form
        public Form1()

        void FillData()
            // 1
            // Open connection
            using (SqlConnection c = new SqlConnection(
                // 2
                // Create new DataAdapter
                using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM EmployeeIDs", c))
                    // 3
                    // Use DataAdapter to fill DataTable
                    DataTable t = new DataTable();

                    // 4
                    // Render data onto the screen
                    // dataGridView1.DataSource = t; // <-- From your designer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.