Reputation: 2369
I've got a DataSet in VisualStudio 2005. I need to change the datatype of a column in one of the datatables from System.Int32
to System.Decimal
. When I try to change the datatype in the DataSet Designer
I receive the following error:
Property value is not valid. Cannot change DataType of a column once it has data.
From my understanding, this should be changing the datatype in the schema for the DataSet. I don't see how there can be any data to cause this error.
Does any one have any ideas?
Upvotes: 24
Views: 27002
Reputation: 41
Its an old Question but it still can happen at VS 2019
Solution:
Now it should be possible to change the type without any problem.
Upvotes: 4
Reputation: 74740
Open With...
XML (Text) Editor
<xs:element name="DataColumn1"
msprop:Generator_ColumnVarNameInTable="columnDataColumn1"
msprop:Generator_ColumnPropNameInRow="DataColumn1"
msprop:Generator_ColumnPropNameInTable="DataColumn1Column"
msprop:Generator_UserColumnName="DataColumn1"
type="xs:int"
minOccurs="0" />
type="xs:int"
to type="xs:decimal"
Run Custom Tool
Upvotes: 0
Reputation: 2369
I have found a work around. If I delete the data column and add it back with the different data type, then it will work.
Upvotes: 3
Reputation: 4595
For those finding this via Google and you have a slightly different case where your table has got data and you add a new column (like me), if you create the column and set the datatype in separate statements you also get this same exception. However, if you do it in the same statement, it works fine.
So, instead of this:
var column = myTable.Columns.Add("Column1");
column.DataType = typeof(int); //nope, exception!
Do this:
var column = myTable.Columns.Add("Column1", typeof(int));
Upvotes: 4
Reputation: 476
I get the same error but only for columns with its DefaultValue
set to any value (except the default <DBNull>
). So the way I got around this issue was:
<DBNull>
Upvotes: 46
Reputation: 3393
Since filled Datatables do not entertain a change in the schema a workaround can be applied as follows:
Make a new datatable
Use datatable's Clone method to create the datatable with the same structure and make changes to that column
In the end use datatable's ImportRow method to populate it with data.
HTH
Upvotes: 5