Question : Convert string to DataTable object

I would like to create up to 10 DataTables on the fly. All of the tables have the same structure. The difficulty I have is creating a DataTable name from a string based on a counter. I have not been able to find a way to convert the string to a DataTable name.

Dim ds As New DataSet
Dim abc As String
Dim counter As Integer
Dim dt As String
dt = “abc” + counter

No matter which way I have tried to covert the string   dt   to a DataTable object, I always come up with the same message—Value of String cannot be converted to System.Data.DataTable- Please help.

Answer : Convert string to DataTable object

WOW!

Whatever you wrote up there does not make much sense at all.

You want to copy the table's structure? That means the table's schema.

Well, what you would want to do is call the Clone method of the original table ten times. Create 10 datatables that have the same schema using the clone method.
Then add those 10 cloned tables to the dataset.

Here is some pseudo code.

Dim ds As New DataSet
'We are assuming there is the original table in the tables collection at index 0
Dim abc As String = "test"

for i as integer = 1 to 10

dim table as datatable = ds.tables(0).clone()
table.tablename = abc & i.tostring()
ds.tables.add(table)

next i


'now you'll have 10 extra tables in the dataset named the way you want, that copy the original table's schema

Random Solutions  
 
programming4us programming4us