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.

Thanks
emmeeAsked:
Who is Participating?
 
SriVaddadiCommented:
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
0
 
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
http://forums.asp.net/t/1528995.aspx 
 

 
0
 
GlobaLevelProgrammerCommented:
Here ya go...use the sqldataadaptor...

http://dotnetperls.com/sqldataadapter-adonet

-=-=-=-=

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

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FillData();
        }

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

                    // 4
                    // Render data onto the screen
                    // dataGridView1.DataSource = t; // <-- From your designer
                }
            }
        }
    }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.