ListBox Multiple Selection
-
The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#
-
The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#
You'll have to create an "or" separated parameter list for the Where Taking your example: "... Where id=1 or id=2 or id=3 " So, I would say a for statement iterating through the array, getting each value and adding "or id=" + value Something along theselines.. I'm not sure if this is clear enough, if you have any questions about it, i'll try to clarify. Andres Manggini. Buenos Aires - Argentina.
-
You'll have to create an "or" separated parameter list for the Where Taking your example: "... Where id=1 or id=2 or id=3 " So, I would say a for statement iterating through the array, getting each value and adding "or id=" + value Something along theselines.. I'm not sure if this is clear enough, if you have any questions about it, i'll try to clarify. Andres Manggini. Buenos Aires - Argentina.
-
The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#
Andres should work but you can shorten it down by using the "IN" keyword.
WHERE id IN ( @id1, @id2, @id3, ... )
I'm pretty sure you'll have to use dynamic sql generation if you try this in a stored proc though. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978 -
The ListBox gets populated by SQL server database (databinding), I need to support multiple selection so that I can retrieve all the selected values in the ListBox and send them back to database as sql statement and then get the result back to show in the web page. My question is: How can I construct the sql statement with parameter (array) that reflect those selections in ListBox? e.g. In sql statement " Where id=@id" works for single parameter, if id is type of int, @id is also type of int. Now I have an id array holding the selected id from ListBox. What will be the array type. How can I construct sql ? Chris#