Solved

Convert string to DataTable object

Posted on 2004-10-05
7
1,334 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
Close form "before" open 3 49
MailAddress in vb 4 29
Must we use a disconnected model when creating database applications? 6 17
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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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