thevan
thevan

Reputation: 10344

How to change the DataTable's Column DataType which already has data using C#.Net?

I have two DataTables which has the following rows.

Table A:

  TaxID               TaxName
 ------              --------
   1                    VAT
   2                    Sales Tax

Table B:

 TaxID                TaxName
 -----                --------
   1A                  ED
   2A                  Discount

In Table A: The datatype for the column TaxID is INT In Table B: The datatype for the column TaxID is String.

Now I want to merge these two tables. I tried like this:
TableA.Merge(TableB); But it gives error. how to achieve this?

Upvotes: 1

Views: 1855

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460048

You cannot change a DataColumn's DataType after the Datatable was populated with data. So you should cast it with SQL to the appropriate type(varchar) before you fill it.

For example:

SELECT CAST(TaxID as varchar(5))AS TaxID, TaxName FROM TableA

Upvotes: 4

Related Questions