Referring to a datatable by string

bd528

Member
Joined
Aug 11, 2012
Messages
13
Programming Experience
Beginner
Hi,

I am trying to run the following code :-

VB.NET:
MyCommand.Fill(dsMain, i)
sqlGaz(i, 0) = dsMain.Tables(i)

sqlGaz is an array, and the value at (i,0) is dtInvoice

But when I try to run the code I get the following error :-

Value of type 'System.Data.DataTable' cannot be converted to 'String'.

How can I call the datatable name "dtInvoice" from the array/loop?
 
How can I call the datatable name "dtInvoice" from the array/loop?

Is this what you mean?

VB.NET:
            For Each table In DS.Tables
                If table.TableName = "dtInvoice" Then
                  'do whatever you are going to do with the data from this DataTable
                End If
            Next
 
Is this what you mean?

VB.NET:
            For Each table In DS.Tables
                If table.TableName = "dtInvoice" Then
                  'do whatever you are going to do with the data from this DataTable
                End If
            Next

In a way...My array has a number of datatable names, one of which is dtInvoice

I have a For loop, incrementing i.

So rather than :-

VB.NET:
[COLOR=#3E3E3E]dtInvoice = [/COLOR][COLOR=#3E3E3E]dsMain.Tables(0)
[/COLOR][COLOR=#3E3E3E]dtTable1 = [/COLOR][COLOR=#3E3E3E]dsMain.Tables(1)
[/COLOR][COLOR=#3E3E3E]dtTable2 = [/COLOR][COLOR=#3E3E3E]dsMain.Tables(2)[/COLOR]

I just want to use

VB.NET:
sqlGaz(i, 0) = dsMain.Tables(i)

but I'm getting the error mentioned above...
 
If you are trying to extract the tables from the DataSet then

Dim dtInvoice as DataTable = dsMain.Tables(0).copy
 
Last edited:
If you are trying to extract the tables from the array then

Dim dtInvoice as DataTable = dsMain.Tables(0).copy

That's true. I was hoping to reduce code by including the table names in my array so I could call them in my loop. Doesn't look like it's possible.
 
TableName is a string and sqlGaz is a string. So why won't this work?
VB.NET:
For each table in DS.Tables
  If table.TableName = sqlGaz(i, 0) Then
    'do something here
  End If
Next
 
That's true. I was hoping to reduce code by including the table names in my array so I could call them in my loop. Doesn't look like it's possible.

If you just want to get the table name into an ArrayList, then....

VB.NET:
Dim i As Integer = 0
For Each Table in DS.Tables
  MyArrayList.Insert(i, DS.Table(i).TableName)
  i += 1
Next
 
Back
Top