By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

Listbox values in SQL Data Source

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.