Solved

Convert string to DataTable object

Posted on 2004-10-05
7
1,302 Views
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.
0
Comment
Question by:luke_100
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
gregasm earned 250 total points
ID: 12233347
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

0
 
LVL 4

Expert Comment

by:eozz_2000
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")
0
 
LVL 8

Expert Comment

by:gregasm
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

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

Author Comment

by:luke_100
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?
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
0
 
LVL 8

Expert Comment

by:gregasm
ID: 12233843
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
0
 

Author Comment

by:luke_100
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

16 Experts available now in Live!

Get 1:1 Help Now