473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Listbox values in SQL Data Source

I've a ListBox with the Selection Mode set to Multiple. I want to use the
Selected values as the WHERE Parameters in a SQL Data Source.
If I set the SQL Data Source to use the Selected Value of the Control, only
the 1st selected value is used.
If I build a TextBox with the selected values & use that as the Parameters
Control, I get the same results.
A SQL query using the IN operator would work, but I've been unable to make
that work.
Any help is appreciated.
Apr 26 '07 #1
5 4819
Hi,
You can loop through all selected items of listbox and append it to
string.Then you can assign that string to sql datasource.Something like this:
Dim strselecteditems As String
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & ListBox1.Items(i).Text

End If
Next
Thereafter set the SQL Data Source to string strselecteditems
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I've a ListBox with the Selection Mode set to Multiple. I want to use the
Selected values as the WHERE Parameters in a SQL Data Source.
If I set the SQL Data Source to use the Selected Value of the Control, only
the 1st selected value is used.
If I build a TextBox with the selected values & use that as the Parameters
Control, I get the same results.
A SQL query using the IN operator would work, but I've been unable to make
that work.
Any help is appreciated.
Apr 27 '07 #2
I added a textbox control & populated it with the selected values. I tried
using values with & without single quotes, neither worked. My query is using
the IN operator & works well (without the quotes) when only one item is
selected.

"Manish Bafna" wrote:
Hi,
You can loop through all selected items of listbox and append it to
string.Then you can assign that string to sql datasource.Something like this:
Dim strselecteditems As String
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & ListBox1.Items(i).Text

End If
Next
Thereafter set the SQL Data Source to string strselecteditems
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I've a ListBox with the Selection Mode set to Multiple. I want to use the
Selected values as the WHERE Parameters in a SQL Data Source.
If I set the SQL Data Source to use the Selected Value of the Control, only
the 1st selected value is used.
If I build a TextBox with the selected values & use that as the Parameters
Control, I get the same results.
A SQL query using the IN operator would work, but I've been unable to make
that work.
Any help is appreciated.
Apr 27 '07 #3
Hi,
I think you are not properly concatenating the string.ICan you post code you
are using for concatenating the string.It should be something like this:
Code for with Single Quotes:
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & "'" & ListBox1.Items(i).Text &
"','"
strselecteditems = strselecteditems.Substring(strselecteditems.Length - 2)
End If
Next

Code for without Single Quotes:
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & ListBox1.Items(i).Value & ","

strselecteditems = strselecteditems.Substring(strselecteditems.Length - 1)
End If
Next

--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I added a textbox control & populated it with the selected values. I tried
using values with & without single quotes, neither worked. My query is using
the IN operator & works well (without the quotes) when only one item is
selected.

"Manish Bafna" wrote:
Hi,
You can loop through all selected items of listbox and append it to
string.Then you can assign that string to sql datasource.Something like this:
Dim strselecteditems As String
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & ListBox1.Items(i).Text

End If
Next
Thereafter set the SQL Data Source to string strselecteditems
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I've a ListBox with the Selection Mode set to Multiple. I want to use the
Selected values as the WHERE Parameters in a SQL Data Source.
If I set the SQL Data Source to use the Selected Value of the Control, only
the 1st selected value is used.
If I build a TextBox with the selected values & use that as the Parameters
Control, I get the same results.
A SQL query using the IN operator would work, but I've been unable to make
that work.
Any help is appreciated.
Apr 28 '07 #4
Hi,
Please do following corrections in my previous post:
code for with quotes
strselecteditems = strselecteditems.Substring(0,strselecteditems.Leng th - 2)
code for without quotes.
strselecteditems = strselecteditems.Substring(0,strselecteditems.Leng th - 1)
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I added a textbox control & populated it with the selected values. I tried
using values with & without single quotes, neither worked. My query is using
the IN operator & works well (without the quotes) when only one item is
selected.

"Manish Bafna" wrote:
Hi,
You can loop through all selected items of listbox and append it to
string.Then you can assign that string to sql datasource.Something like this:
Dim strselecteditems As String
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & ListBox1.Items(i).Text

End If
Next
Thereafter set the SQL Data Source to string strselecteditems
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I've a ListBox with the Selection Mode set to Multiple. I want to use the
Selected values as the WHERE Parameters in a SQL Data Source.
If I set the SQL Data Source to use the Selected Value of the Control, only
the 1st selected value is used.
If I build a TextBox with the selected values & use that as the Parameters
Control, I get the same results.
A SQL query using the IN operator would work, but I've been unable to make
that work.
Any help is appreciated.
Apr 28 '07 #5
Here is the code snippet that builds the values without quotes:
Dim i As Integer
Dim n As Integer = lbLog.Items.Count
tbEventLogs.Text = ""
For i = 0 To n - 1
If lbLog.Items(i).Selected Then
tbEventLogs.Text = tbEventLogs.Text & lbLog.Items(i).Text &
","
End If
Next
If Len(tbEventLogs.Text) 0 Then
tbEventLogs.Text = Left(tbEventLogs.Text, Len(tbEventLogs.Text)
- 1)
End If

I ran SQL Profiler & captured a transaction with 2 values:
exec sp_executesql N'SELECT [CategoryID], [Category] FROM [tblCategory]
WHERE [LogID] in (@LogID2)',N'@LogID2 nvarchar(7)',@LogID2=N'APP,DIR'
"Manish Bafna" wrote:
Hi,
Please do following corrections in my previous post:
code for with quotes
strselecteditems = strselecteditems.Substring(0,strselecteditems.Leng th - 2)
code for without quotes.
strselecteditems = strselecteditems.Substring(0,strselecteditems.Leng th - 1)
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"BigSam" wrote:
I added a textbox control & populated it with the selected values. I tried
using values with & without single quotes, neither worked. My query is using
the IN operator & works well (without the quotes) when only one item is
selected.

"Manish Bafna" wrote:
Hi,
You can loop through all selected items of listbox and append it to
string.Then you can assign that string to sql datasource.Something like this:
Dim strselecteditems As String
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
strselecteditems = strselecteditems & ListBox1.Items(i).Text
>
End If
Next
Thereafter set the SQL Data Source to string strselecteditems
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
>
>
>
"BigSam" wrote:
>
I've a ListBox with the Selection Mode set to Multiple. I want to use the
Selected values as the WHERE Parameters in a SQL Data Source.
If I set the SQL Data Source to use the Selected Value of the Control, only
the 1st selected value is used.
If I build a TextBox with the selected values & use that as the Parameters
Control, I get the same results.
A SQL query using the IN operator would work, but I've been unable to make
that work.
Any help is appreciated.
Apr 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon P | last post by:
Hello group, I'm in desperate need of help. Here goes : I have the following tables : CONTACTS (ContactID, FirstName, LastName, Company, etc.), SHOWS (ShowID, ShowDescription) and SHOWDETAILS...
3
by: Paul W | last post by:
I am populating listbox with a datreader. DateReader sqlcommand is dynamic, based on several user selections Listbox DataValueField is set to the tables primary key Listbox DataTextField is...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
1
by: JMann101 | last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I have a datagrid which list users and when an admin clicks "edit" a defined column becomes visible and a dynamic listbox control...
13
by: Larry Woods | last post by:
I am creating a "from-to" set of listboxes where the "left" listbox had a list of values and I want to be able to select these values, 1 at a time, and move them into a "right" listbox, removing...
1
by: nasirmajor | last post by:
i have a databound listbox that has a multilple selection made to true. when user select multiple values in listbox and clicks button i want them to show the results in the datagrid on the same...
1
by: eric | last post by:
Hi, Can anyone please explain to me how I can cop selected items from a multi listbox(simple) to another multi listbox? the code I found on msdn only seems to support single column copies, i...
1
by: Refugnic | last post by:
I tried to fill a ListBox with a DataSource pointing to an ArrayList. It all works fine...up to one point. The ArrayList is dynamic, which means the contents of it change, during the course of...
3
by: Renilkumar | last post by:
Hi, I am using .net 2.0. I have a .aspx page with vb.net as codebehind. My form has one dropdown, 2 listboxes with add & remove button. During pageload I am loading all the values from the db to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.