Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1350
  • Last Modified:

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.
0
luke_100
Asked:
luke_100
  • 3
  • 2
  • 2
1 Solution
 
gregasmCommented:
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
 
eozz_2000Commented:
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
 
gregasmCommented:
He says
" All of the tables have the same structure"

instantiating a new datatable and setting the name does no fulfill his requirement.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eozz_2000Commented:
Ok man, please, don't upset, I didn't want to make you feel attacked :)
0
 
luke_100Author Commented:
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
 
gregasmCommented:
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
 
luke_100Author Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now