469,604 Members | 2,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

Cannot change DataType of a column once it has data.

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?
Nov 17 '05 #1
2 44544
ad wrote:
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"));

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

de*******@freelancedotnetdeveloper.com
http://www.freelancedotnetdeveloper.com
Providing consultancy services in .NET
Nov 17 '05 #2
ad wrote:
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"));

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

de*******@freelancedotnetdeveloper.com
http://www.freelancedotnetdeveloper.com
Providing consultancy services in .NET
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by c duden | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.