Convert string to DataTable object

Posted on 2004-10-05
Last Modified: 2012-06-27
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.
Question by:luke_100
  • 3
  • 2
  • 2

Accepted Solution

gregasm earned 250 total points
ID: 12233347

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()

next i

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


Expert Comment

ID: 12233365
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")

Expert Comment

ID: 12233472
He says
" All of the tables have the same structure"

instantiating a new datatable and setting the name does no fulfill his requirement.
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


Expert Comment

ID: 12233659
Ok man, please, don't upset, I didn't want to make you feel attacked :)

Author Comment

ID: 12233756
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?

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?


Expert Comment

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


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)

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


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

Author Comment

ID: 12237119
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.

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now