23

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?

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
firedfly
  • 2,299
  • 2
  • 22
  • 20

6 Answers6

46

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:

  1. Column DefaultValue : Type in <DBNull>
  2. Save and reopen the dataset
Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
Are
  • 476
  • 5
  • 3
  • 2
    This works, but something should be noted: there is a validation done at the time you save and close. If there is a problem then the fields are simply deleted when you close the dataset. I found this out the hard way as I lost a lot of information I entered. To prevent loss, be sure the dataset saves without errors BEFORE you try to close it. – DAG Mar 23 '16 at 16:48
  • lol you really have to save and close then open for this to work, thanks life saver – Ahmed Mohammed Jun 07 '18 at 13:06
  • Thanks, this works :). "Save and reopen" DataSet is must for this to work. – Vaibhav Gawali Oct 11 '18 at 05:16
5

Since filled Datatables do not entertain a change in the schema a workaround can be applied as follows:

  1. Make a new datatable

  2. Use datatable's Clone method to create the datatable with the same structure and make changes to that column

  3. In the end use datatable's ImportRow method to populate it with data.

HTH

Codeslayer
  • 3,383
  • 7
  • 35
  • 42
4

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));
ldam
  • 4,412
  • 6
  • 45
  • 76
3

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.

firedfly
  • 2,299
  • 2
  • 22
  • 20
  • I have run into the same problem. Strange - can't change it in the designer. Looks like a bug... – aSkywalker Nov 21 '09 at 15:01
  • Same bug is still in VS2010. Same solution works. Remove the default values, save and close the DS, then re-open and you can edit the data types. – Andy Dec 21 '11 at 11:37
2

Its an old Question but it still can happen at VS 2019

Solution:

  1. Change the DefaultValue to <DBNull>
  2. Save the Dataset
  3. Close the DataSet Designer
  4. Re-Open the Designer

Now it should be possible to change the type without any problem.

0
  • Close the DataSet in the visual designer
  • Right click the dataset, choose Open With...
  • Choose XML (Text) Editor
  • Find the column in the XML, in your dataset it will look something like:
<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" />
  • Change the type="xs:int" to type="xs:decimal"
  • Save and close the XML editor
  • You may need to right click the DataSet again and choose Run Custom Tool
Caius Jard
  • 72,509
  • 5
  • 49
  • 80