Link to home
Start Free TrialLog in
Avatar of kwitcom
kwitcomFlag for United States of America

asked on

Dataset convert into array

I have a dataset that the user has changed in a Datagrid.  I want to take one column of data, i.e. sales order # and get an array so I can use the list in another SQL lookup for more details on the order number.

Sample
ord_no    Cust_no    Address
1              3334           1 Test Dr.
2               3336           5 Test Dr.
6              3339            9 Test Dr.

Now I need to get the ord_no from the dataset as the following
(1, 2, 6)
Reason is so I can place into SQL command Select * from Order_details where ord_no in (1, 2, 6)
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Just create an array of the correct size (and type), and then just loop through each row of the table in the dataset & populate the array
Dim table As DataTable = ds.Tables("TableName")
Dim values As New Integer(table.Rows.Count -1)
For i As Integer = 0 to values.Length - 1
  values(i) = CInt(table.Rows(i)("ord_no"))
Next

Open in new window

You need to loop through the dataset to create an array
ASKER CERTIFIED SOLUTION
Avatar of srikanthreddyn143
srikanthreddyn143

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're going to concatenate a string in a loop of an unknown (at design time) number of items, I'd highly recommend using either StringBuilder or creating a String array, and using Join.

The reason being that a string is immutable, ie. a new copy is created every time you modify it.

Of course, don't go overboard with StringBuilder, if you know the loop will be only 3 items there's no need for it. If there could be 100+ items, it's probably worth using.
Dim values As New String(table.Rows.Count - 1)
'' Populate String array
 
Dim query As String = "SELECT ... IN (' & String.Join(",", values) & ")"

Open in new window