472,127 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

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 45402
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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.