Using selected values from a Multi Select Listbox in a SQL query

I have seen a few post concerning this but don't really understand fully as I am new to this.  I would appreciate any help

1) First I loop through the Listbox control and get the selected values:

Dim li As ListItem

For Each li In lbPClass.Items

If li.Selected = True Then

strPC += li.Value & ", "

End If

Next

Label5.Text = strPC

The resulting string looks something like: 101, 102, 100 ,    I get rid of the last space and "," with the following statement

Label6.Text = Left(Label5.Text, Trim(Len(Label5.Text) - 2))

2) I would then like to use this in a SQL query using the IN statement based on a parameter called @PROPCLASS

The parameter is defined using:

Cmd.Parameters.Add(New OleDbParameter("@PROPCLASS", Label6.Text))

Here is my sql statement that ONLY works if either I only select one value from the listbox or I replace the parameter with IN( 101, 102, 100)

strSQL = "SELECT * FROM web_transfer WHERE ( PROP_CLASS IN (@PROPCLASS)) AND (ACRES >= @lowSize)AND (ACRES < @HighSize) AND (YEAR = @lowYear)AND (SALE_PRICE > @lowPrice) AND (SALE_PRICE < @highPrice) "

Please someone, how do I add this information into my SQL statement?  I have been pulling out my hair over this all afternoon and although I have leant some really useful things in the process I haven't made any headway.  I would appreciate any help.  Thanks in advance,

s

0
crookcounty
1/9/2008 11:59:30 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
1818 Views

Similar Articles

[PageSpeed] 2

It may no be actual solution when u want to use in operator  we may not fix how many values we are going to pass. if its fixed one u can pass them as the parameters

cmd=new sqlcommand("select * from emp where sal in(@sal,@sal1,@sal2)",con)

cmd.parameters.add("@sal", sqldbtype.int).value=20

- - - - - - - -

- - - - - - -

Thank u

Baba

Please remember to click "Mark as Answer" on this post if it helped you.
 


Thank u

Baba

Please remember to click "Mark as Answer" on this post if it helped you.
0
srijaya_ramaraju
1/10/2008 5:34:27 AM

Thanks Baba, but I don't know how many parameters to add.  It will change each time.  Can I do this dynamically??

 

steve

0
crookcounty
1/10/2008 2:30:16 PM

i think yes we can  do it

Thank u

Baba

Please remember to click "Mark as Answer" on this post if it helped you.

 


Thank u

Baba

Please remember to click "Mark as Answer" on this post if it helped you.
0
srijaya_ramaraju
1/10/2008 2:39:13 PM

Hi Steve,

I had similar sort of problem, but i was using datasets.May be this logic helps u a little

So, i collected all the selected items from List into Array.

then

for (int i=0; i<Array.GetLength(0); i++)

{

 int result = TableAdapater.insert(Array[i]);

since insert gives me a return value , i used result to hold it.Apart from that its of no use.

}

0
sensa_10d
1/10/2008 10:04:36 PM
Reply:

Similar Artilces:

Pre select records in multi select listbox using FindbyText
Hi Everyone, I am having problems in preselecting the multiselect listbox depending on the value in database. I have listbox for displaying all the languages and want them to preselect values from database before updating them. Database field: language stores the value along with delimiters like French;Dutch;Spanish. I would like to separate the delimiters and pre-select the values in listbox (lbLanguage). I am using datareader. Please let me know what is wrong in the following code. My Code: string langList=dreader[19].ToString(); string[] langArray=langList.Split(';')...

Multi-select ListBox and Select All
Hi,I'm having an issue with some JS code for IE6 (no need for x-browser) and multi-select listbox.I'm trying to implement a Select All button.Current code is : function SelectAll(obj){ for (var i = 0; i < obj.options.length; i++) { obj.options[i].selected = true; } } which logically seems to work.  The problem I have is that this does not seem to work until I click the multi-select again.  It seems that although the options are marked as selected, the screen does not update to reflect this until something triggers it (in this case trying to click in ...

Using a Multi-select Listbox as an SQL parameter for a GridView
Hi All,I'm confused. Using VWD2005 with VB.NET. Using a multiple select Listbox as a paramater in the SQL statement that populates the gridview.First of all I'm surprised that since the gridviews sqldatasource wizard lets you step through all the way to selecting a listbox control as a parameter in a SQL statement, Surely the wizard should automatically detect that the selected listbox is multi-select and asp should generate the code to handle multiple selected items? Anyway, I've read various posts and cannot establish what the correct ASP.NET solution is? This is ...

Multiple-Select Listbox only returns first selected value (and I'm not using SelectedItem...)
I'm looping through the items of a listbox, as you can see I'm raising a custom event, passing the selected values with custom event args up...  What happens however, is that only the first item will be tagged as being selected, no matter how many listbox items I select with control. Ie If I select "Transducer, Pressure Transmitter, Pressure & Temperature" the following is returned: Transducer is selected;Pressure transmitter is not selected;Pressure & Temperature is not selected; I encoded the ampersand & so do not worry about string concat. This is t...

Select Statement using multi-list box values for WHERE IN SQL Clause
I have a gridview that is based on the selection(s) in a listbox.  The gridview renders fine if I only select one value from the listbox.  I recive this error though when I select more that one value from the listbox: Syntax error converting the nvarchar value '4,1' to a column of data type int.  If, however, I hard code 4,1 in place of @ListSelection (see below selectCommand WHERE and IN Clauses) the gridview renders perfectly. <asp:SqlDataSource ID="SqlDataSourceAll" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="S...

On selecting value in second listbox. selected value in first textbox is lost
     Hello all, In a webapplication , I have to store the selected values in hidden fields with following code  <INPUT type="hidden" name="hidPatient" value=""> <INPUT type="hidden" name="hidPatientStatus" value="">  when i select the value in any list box following javascript function is called with this code   Response.Write "<SELECT id=" & strSelName & " name=" & strSelName & " validation=" & strValidation & " ...

select cheboxlist if any values are selected in sql
hello and thanks in advance,  I have a checkboxlist that is created dynamically from table1 once the records are inserted they are inserted in table2. i can save the values into the table and remove them if i uncheckd them that is working fine. the problem is to checked the items that have been selected already, how to i do this?? really really confuse...   here is the cs code; namespace IMS_2K9.Admin { public partial class Order_Charges : System.Web.UI.Page {private string connectionString = WebConfigurationManager.ConnectionStrings["IMSCon...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

How Select one dropdownlist value using down arrows corresponding other dropdownlist value are select
I am using two dropdownlist box.One is used for country selection. other one is used for state selection.I am using dropdown selectedindexchanged event. when i click country dropdownlist corresponding state is selected in state dropdownlist box. How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.R.Karthikeyan   kartmca: How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.Generally, we don't implmenet...

Use value from one database query to select second query
 Hi, I have the following code, which looks up the value 'comment_id' from a database table (tblPage), then uses that value to look up values from the comments table (tblComment):  protected void Page_Load(object sender, EventArgs e)    {        SqlConnection conn = new SqlConnection("Server=my.server.ip;Database=db;User ID=myusername;Password=mypassword");        conn.Open();                //Page&nbs...

value of multi select listbox
Hey Guys, I have 2 listboxes on a page used for adding and removing from an available list to a selected list. I have the value property of my available list set to id's from a database. When i select multiple items at the same time from listbox 1 and move them to listbox 2, i am always getting the same value for all of the items in listbox 2. Whats worse is that instead of getting the datavaluefield, i get the index of listbox 1 as the value field. For example: I choose items 2,3,5 from listbox 1 and when they are moved to listbox 2, my item.value is always 2 instead of having...

Use value from select query
I am writing to learn if it is possible to obtain a value from a select query, and utilize this value in a succeeding query. Please see below for my current query.  I would like to take the filenum result, and use the value in a second query. Thanks in advance!  select shhd.CUST_NO, shhd.file_no as filenum from tpsdba.shipment_header SHHD WHERE SHHD.CUST_NO = '052584 ' AND (SHHD.DATE_ENTRY >= '20080401' AND SHHD.DATE_ENTRY <= '20080404')    declare @Result as nvarchar(10) set @Result = (select shhd.CUST_NO, shhd.file_no as fil...

Listbox Multi Selected Value
I am trying to get the values selected in a list box on my web form; and use these value in an sql statement. I have done this within a windows form but i am struggling to work out how to achieve this in a web form; the windows form commands are not liked in the asp!!!  Any help or advice would be appreciated. here is a small example function  protected void Button1_Click(object sender, EventArgs e) { int count = ListBox1.Items.Count; for (int i = 0; i < count; i++) { if (ListBox1.Items[i].Selected == true) { ...

SQL Keyword is used as Field Name. SELECT Query Problem using DBI.
Hello, One of our tables awb_msg has the field name 'text' and when I try to use the DBI to do a select it prompts an error. How can I do this. I can't change the field name as it is in a production environment and has been use for the past 10 years with no problems. We are using ms- sqlserver. And also, I have no control of changing the field name. So, changing the field name is not possible. Here is my test: This is working correctly. -- my $sql = "SELECT seq, create_date, msg_code, send_receive FROM awb_msg where seq = 7989176"; it returns: ...

Web resources about - Using selected values from a Multi Select Listbox in a SQL query - asp.net.presentation-controls

Portal:Law/Nominate/Selected article - Wikipedia, the free encyclopedia
Add the article in to a subpage of this portal, such as Portal:Law/Selected article/# , replacing the "#" with the number above the current highest. ...

F8: Facebook’s FbStart Initiative Will Give Selected Developers A $30K Kick-Start
... successful applications is likely a whole lot easier with $30,000 in free tools and services to start off with, and that’s exactly what selected ...

LEGO Shop - The LEGO Technic team has selected the winner... - Facebook
The LEGO Technic team has selected the winner of the Co-Creation Challenge! LEGO will be producing a limited run of 20,000 of this stunning set.... ...

baby365- Grand Doctor Ma Selected Students for iPad on the iTunes App Store
Get baby365- Grand Doctor Ma Selected Students on the App Store. See screenshots and ratings, and read customer reviews.

Selected Cheney Vice Presidential Photographs
... for photos found within these albums are Paul Morse, Karen Ballard, Kimberlee Hewitt, Shealah Craighead, and Chris Greenberg. These selected ...

[iOS Advice] How To Clarify Your Email Replies By Quoting Selected Text - YouTube
Follow me on Twitter: http://www.twitter.com/macmixing Like my Facebook page: http://www.facebook.com/macmixing RATE, COMMENT, SUBSCRIBE! :-) ...

Local farmers to sell their fresh dates at selected supermarkets
Fresh dates will be on sale this summer under the label of Local Harvest at selected supermarkets and Spinney's.

Animal of the Day: Lucius Has Been Selected as the U.K.’s ‘Happiest Cat’
He’s got one blue eye, one green eye and a heart of gold. British mobile phone company O2 recently held some “cat auditions” to find “the UK’s ...

Michael Sam becomes first openly gay player selected in an NFL Draft
Defensive end Michael Sam of the University of Missouri became the first openly gay player to be selected in a National Football League Draft ...

Artist facing child pornography charges selected for MCA exhibition
An artist facing child pornography charges has been selected to show work in a prestigious exhibition at the Museum of Contemporary Art Australia. ...

Resources last updated: 1/14/2016 1:35:35 PM