Cannot change DataType of a column once it has data.

A

ad

I use ado.net to fill a Excel wroksheet into a DataTable.

The data in the Excel wroksheet is digital.
After the data filled into the DataTable, the DataType of each column is set
to
Double, but I want to treat them as string, I use the code to covert them

DataTable dt = ds.Tables0];
int iColCount = dt.Columns.Count;
for (int j = 0; j < iColCount; j++)
{
DataColumn myDC = dt.Columns[j];
myDC.DataType = System.Type.GetType("System.String");
}

But it fail with message Cannot change DataType of a column once it has
data.

How can I do?
 
D

DotNet Developer

ad said:
I use ado.net to fill a Excel wroksheet into a DataTable.

The data in the Excel wroksheet is digital.
After the data filled into the DataTable, the DataType of each column is set
to
Double, but I want to treat them as string, I use the code to covert them

DataTable dt = ds.Tables0];
int iColCount = dt.Columns.Count;
for (int j = 0; j < iColCount; j++)
{
DataColumn myDC = dt.Columns[j];
myDC.DataType = System.Type.GetType("System.String");
}

But it fail with message Cannot change DataType of a column once it has
data.

How can I do?
-----------------------------------------------------------------------
-----------------------------------------------------------------------

Do the following in this part:
DataColumn myDC = dt.Columns[j];
myDC.DataType = System.Type.GetType("System.String");

!!! Use this instead !!!!
-----------------------------------------------------------------------

DataColumn myDC = new
DataColumn(dt.Columns[j].ColumnName,System.Type.GetType("System.String"));

-----------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top