Why can't I access tables in a dataset by table name?

anilgupte

Member
Joined
Mar 7, 2007
Messages
11
Programming Experience
1-3
In my VB.Net desktop application, I have several places where I am accessing a table, both for reading and for updating. For example:

Dim tempCount As Integer = Glbl.GlobalDataSet.Tables("Profiles").Rows.Count

The above does not work. However, the following does:

Dim tempCount As Integer = Glbl.GlobalDataSet.Tables(4).Rows.Count

I am pretty sure it was working in th past (I have recently switched to VS 2010 - could this have anything to do with it?) but it is not working now.

How can I make the first statement work, so can access tables by table name instead of by index?

Thanx for your help.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
Is it a typed or untyped DataSet? If it's typed, why are you using the Tables collection at all when the DataSet has a dedicated property for each table? If it's untyped, did you give the DataTable a name when you populated it?
 

anilgupte

Member
Joined
Mar 7, 2007
Messages
11
Programming Experience
1-3
Not sure what you mean by typed/untyped but I declared it originally as
Public Shared GlobalDataSet As DataSet

I have a function that reads a text file and creates tables and populates them and assigns them to GlobalDataSet. And I uses the following to add tables (example):
TableName = strRead.Substring(x + 6)
ds.Tables.Add(TableName)

So, I think that's a yes to your questions?

Thanx!
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
What do you see if you do this?
For Each table As DataTable In Glbl.GlobalDataSet.Tables
    MessageBox.Show(table.TableName)
Next
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
Then I can only assume that either you specified the wrong table name or else something somewhere is corrupt. If it's the latter then what and where is anyone's guess. Are you sure that you don't have spaces in the names or something like that? What if you change that code snippet I gave you to this:
For Each table As DataTable In Glbl.GlobalDataSet.Tables
    MessageBox.Show("|" & table.TableName & "|")
Next
By the way, your profile still says .NET 1.1 so, if you have upgraded, you should change that.
 

anilgupte

Member
Joined
Mar 7, 2007
Messages
11
Programming Experience
1-3
I don't know what happened, but it has mysteriously started working again. Sheesh! Maybe it was some trailing or leading spaces somewhere.
Thanx for your help!
 
Top Bottom