doglin
doglin

Reputation: 1741

why can't I pass the name of the table to get the table from DataSet.tables?

I have a custom table. Here is the information about the custom table. Here are the names of the table

Table Name : customtable_TwitterCacheTable

Why can't I access the table by using the Table Name,

so in my code, if I do this, I will get a NullReference Exception

   foreach (DataRow row in ds.Tables["customtable_TwitterCacheTable"].Rows)
                    {
                    CustomTableItem deleteItem = new CustomTableItem(row, customTableClassName);

                    deleteItem.Delete();

                }

But the following seems work OK

   foreach (DataRow row in ds.Tables[0].Rows)

Why can't I pass the name of the table in to get a table? Do I have to use this index based approach?

Please let me know.

Thanks

Upvotes: 0

Views: 73

Answers (1)

Mike Schwartz
Mike Schwartz

Reputation: 2212

You need to manually name the table in the dataset. Unless its excplicitly named, its only known as ds.tables[0]. Depending on how you get the data you could:

//if you used a SqlDataAdapter
da.Fill(ds, "customtable_TwitterCacheTable");

Or you could do this:

ds.Tables[0].TableName = "customtable_TwitterCacheTable";

Upvotes: 2

Related Questions