473,396 Members | 2,011 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,396 software developers and data experts.

select *...where x in (" & obj & ")" -- what collection obj can I

I need to build a sql string that looks like this:

strSql = "Select * from tbl1 Where x In (123,456,789)"
or
strSql = "Select * from tbl1 Where x In (123,456,789,527,914)"

The numbers represent RecordID's from rows I will select from a
datagridview. I tried storing these values in an arrayList, and even a
string array and tried this:

s1 = New ArrayList
For Each row As DataGridViewRow In SelectedRows
s1.Add(row.Cells("DetailID").Value.ToString)
Next
....
strSql = "Select * from tbl1 Where x In (" & s1 & ")

VS complained until I added s1.ToString, which of course, did not work.

strSql = "Select * from tbl1 Where x In (" & s1.ToString & ")

My alternative is to build a "Where" string

For i As Integer = 0 to s1.Count - 1
str1 += s1(i).ToString & ","
Next
strSql = "Select * from tbl1 Where x In (" & str1 & ")"

This just seems a bit kludgy. I am pretty sure I have dealt with this
before, I just can't remember what object I used.

Any suggestions appreciated.

Thanks,
Rich

Apr 25 '07 #1
3 1775
Like this?
strSql = "Select * from tbl1 Where x In (" & String.Join(",",
s1.ToArray(Type.GetType("System.String"))) & ")
Regards
Matthias

"Rich" <Ri**@discussions.microsoft.comschrieb im Newsbeitrag
news:B4**********************************@microsof t.com...
>I need to build a sql string that looks like this:

strSql = "Select * from tbl1 Where x In (123,456,789)"
or
strSql = "Select * from tbl1 Where x In (123,456,789,527,914)"

The numbers represent RecordID's from rows I will select from a
datagridview. I tried storing these values in an arrayList, and even a
string array and tried this:

s1 = New ArrayList
For Each row As DataGridViewRow In SelectedRows
s1.Add(row.Cells("DetailID").Value.ToString)
Next
...
strSql = "Select * from tbl1 Where x In (" & s1 & ")

VS complained until I added s1.ToString, which of course, did not work.

strSql = "Select * from tbl1 Where x In (" & s1.ToString & ")

My alternative is to build a "Where" string

For i As Integer = 0 to s1.Count - 1
str1 += s1(i).ToString & ","
Next
strSql = "Select * from tbl1 Where x In (" & str1 & ")"

This just seems a bit kludgy. I am pretty sure I have dealt with this
before, I just can't remember what object I used.

Any suggestions appreciated.

Thanks,
Rich

Apr 25 '07 #2
Well here is one other option I remembered that makes it so I can use a
string array

strSql = "Select * from tbl1 Where x In (" & String.Join(",", s1) & ")"

This doesn't seem any less kludgy than using a straight string object
though, because I have to resize the string array, I have to add a counter
var to the for each loop, and I have to use String.Join in the sql statement.
But -- it is another option.
"Rich" wrote:
I need to build a sql string that looks like this:

strSql = "Select * from tbl1 Where x In (123,456,789)"
or
strSql = "Select * from tbl1 Where x In (123,456,789,527,914)"

The numbers represent RecordID's from rows I will select from a
datagridview. I tried storing these values in an arrayList, and even a
string array and tried this:

s1 = New ArrayList
For Each row As DataGridViewRow In SelectedRows
s1.Add(row.Cells("DetailID").Value.ToString)
Next
...
strSql = "Select * from tbl1 Where x In (" & s1 & ")

VS complained until I added s1.ToString, which of course, did not work.

strSql = "Select * from tbl1 Where x In (" & s1.ToString & ")

My alternative is to build a "Where" string

For i As Integer = 0 to s1.Count - 1
str1 += s1(i).ToString & ","
Next
strSql = "Select * from tbl1 Where x In (" & str1 & ")"

This just seems a bit kludgy. I am pretty sure I have dealt with this
before, I just can't remember what object I used.

Any suggestions appreciated.

Thanks,
Rich
Apr 25 '07 #3
Yes. Exactly. This is what I came up with also, so I guess this is the
consensus. And the reason I need some kind of collection object is because
in the rest of this particular process I have to loop through the object
after I build the sql string and retrieve rows. With the string array I can
use For Each Next.

"Matthias Vastring" wrote:
Like this?
strSql = "Select * from tbl1 Where x In (" & String.Join(",",
s1.ToArray(Type.GetType("System.String"))) & ")
Regards
Matthias

"Rich" <Ri**@discussions.microsoft.comschrieb im Newsbeitrag
news:B4**********************************@microsof t.com...
I need to build a sql string that looks like this:

strSql = "Select * from tbl1 Where x In (123,456,789)"
or
strSql = "Select * from tbl1 Where x In (123,456,789,527,914)"

The numbers represent RecordID's from rows I will select from a
datagridview. I tried storing these values in an arrayList, and even a
string array and tried this:

s1 = New ArrayList
For Each row As DataGridViewRow In SelectedRows
s1.Add(row.Cells("DetailID").Value.ToString)
Next
...
strSql = "Select * from tbl1 Where x In (" & s1 & ")

VS complained until I added s1.ToString, which of course, did not work.

strSql = "Select * from tbl1 Where x In (" & s1.ToString & ")

My alternative is to build a "Where" string

For i As Integer = 0 to s1.Count - 1
str1 += s1(i).ToString & ","
Next
strSql = "Select * from tbl1 Where x In (" & str1 & ")"

This just seems a bit kludgy. I am pretty sure I have dealt with this
before, I just can't remember what object I used.

Any suggestions appreciated.

Thanks,
Rich

Apr 25 '07 #4

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

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
3
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
7
by: DC Gringo | last post by:
I am having a bear of a time with setting a URL query string as a text value in a dropdownlist and Server.URLEncode does not seem to do its job. theFullLink = theLinkPrefix &...
5
by: VB Newbie | last post by:
I am creating a user control containing a combobox using VB.NET(2003) I want to add 2 public properties "DataSource" and "Items" like the "System.Windows.Forms.ComboBox" here is my code, but it...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
2
by: pagoto123 | last post by:
select * from table where director = '" & Director.text & "' i want to display me all results that start with the director that the user will enter........ please i need the command help me...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.