user2103442
user2103442

Reputation: 41

Create (in loop) dataset with multiple tables

I need to do create several (I don't know how many until runtime) tables and add each one to a dataset. Each datatable needs to have a different name which can be constructed based on the loop variable.

sub fillDataTableA(byref dt as datatable)
...  code to fill table
end sub
sub fillDataTableB(byref dt as datatable)
...  code to fill table
end sub

loop through branches
   dim dt  (name it as "branch1 A")      '  "branch#" comes from loop
  fillDataTableA(dt)
   dataset.add  (dt)          ' add this table to dataset

   dim dt  ( name it as "branch1 B")
   fillDataTableB(dt)     
   dataset.add  (dt)   ' add second table for this branch
next  (branch)

processDataSets(dataset)

So, if there are 4 branches, there will be 8 tables added to datasets. My problem is I don't know how to name them (tables) differently and dynamically to be added to the dataset.

Can you see how to fix this? Thanks!

Upvotes: 2

Views: 1736

Answers (2)

Sparers
Sparers

Reputation: 433

Is this what you need?

Dim ds As New DataSet

for i as integer=0 to 9

 Dim dt As New DataTable("NameOfTable" & i.tostring)
 ds.Tables.Add(dt)

Next

Upvotes: 0

peterG
peterG

Reputation: 1641

is this enough to get you started?

    Dim n As Integer
    Dim ds As New DataSet

    Dim s = InputBox("Number of tables?")
    n = Integer.Parse(s)

    For i = 1 To n
        Dim t As New DataTable With {.TableName = "newtable" & i.ToString}
        ds.Tables.Add(t)
    Next

Upvotes: 2

Related Questions