Link to home
Start Free TrialLog in
Avatar of luke_100
luke_100

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of gregasm
gregasm

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
Avatar of eozz_2000
eozz_2000

you don't need to create 10 tables to know that you can create a table with an especific name with this:

dim dt as new DataTable("NameOfTheTable")
He says
" All of the tables have the same structure"

instantiating a new datatable and setting the name does no fulfill his requirement.
Ok man, please, don't upset, I didn't want to make you feel attacked :)
Avatar of luke_100

ASKER

OOPs-      not as in object oreinted programming, but I made a slip by responding with my questions to through the member feedback. Did I say that I was new to this process?
Sorry

I am still trying to finish this off. Thank you gregasm, you have put me on the right track. If creating an index table is the right way to go, how do I create it?

Thanks
eozz_2000, I am sorry I gave you that impression, it was not my intention, my apologies.

luke_100,

here are two ways you might create the original table's, the table at index 0's, schema.

You can use a dataadapter and call the fill method on the datatable.

dim myds as dataset = new dataset()
dim table as datatable = new datatable("index table")
dim da as sqldataadapter = new sqldataadapter("select * from customers", conn)
 da.fill(table)
myds.tables.add(table)

Now we have the original table at index 0.

Another way to do this is more manual.

dim table as datatable = new datatable("index table")
with table

.columns.add(new datacolumn("col one")
.columns.add(new datacolumn("col two")

'and etc..

end with

myds.tables.add(table)

There are also other properties of the datacolumn you may want to set, such as column datatype. This gives you more strong  typing support.

When you call myds.tables(0).clone   it will return a datatable object that has all the same column information of the original table, but none of the data in it.

If you call myds.tables(0).copy then you will return a datatable object that is an exact copy of the original table. this is the same as calling Clone(), but it also copies the data inside.  I know this wasnt part of your question. but just an aside that might be helpful to you somewhere down the line.

good luck
Thank you gregasm-- as I am new to programming it will take me a while to test your answer and check it out with my application. It looks like it will work. I am still not sure on the way to actually set a table as index (0). I understand how to create a table. But it is possible to have several tables- how do you designate one of them to index (0). And, how is it possible to add one table and then another with ds.tables.add(table). Even though the tables have different names, I still come up with an error that the table already exists.  Thank you for your help.