Link to home
Start Free TrialLog in
Avatar of emmee
emmee

asked on

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
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial