Dave Hampel
Dave Hampel

Reputation: 168

DataTable TableName = DataView.ToTable("TableName") will not work

I am trying to make a copy of a DataTable with a different TableName. I have used this in the past without issue and the code from the past works just fine today. When i try using the exact same code the table will add to the DataSet but there is no data or column names.

DataView dv = dataSet.Tables["Traveler"].DefaultView;   /// the dataview is exactly like the datatable
DataTable Parts_Kit = dv.ToTable("Parts_Kit");          /// the new datatable has no data or columns
dataSet.Tables.Add("Parts_Kit");                        /// Parts_Kit is added to dataset but completly blank

I have dorked around with this for 4 hours now with no results. What could i possibly be missing?

Upvotes: 0

Views: 653

Answers (1)

Jawad
Jawad

Reputation: 11364

as per the definition of Add(string DataTable name), you are creating a new / blank table in dataSet that has nothing to do with the DataTable Parts_Kit.

As per MS documentation as well, you want to use,

dataSet.Tables.Add(Parts_Kit); // without quotes around Parts_Kit.

This will add the table you created from view (Parts_Kit) to the DataTableCollection, dataSet. To Test this as well, you can print Parts_Kit.TableName to see if it is the correct "Parts_Kit" name (from ToTable("Parts_Kit") command).

Upvotes: 1

Related Questions