Last post Nov 26, 2014 07:44 AM by Rion Williams
Nov 26, 2014 07:35 AM|rohitpundlik|LINK
I am trying to change the datatype of string column to byte column in my dataset; but it is not allowing me to add the column because of following error
Cannot change DataType of a column once it has data.
However, my string column does not have any data; its null. Then why still I am not able to change the column data type ??
ds.Tables.Rows["PwdHash"] = DBNull.Value;
ds.Tables.Columns["PwdHash"].DataType = typeof(System.Byte);
I am not able to understand how to resolve this ???
Nov 26, 2014 07:44 AM|Rion Williams|LINK
You have two options here I believe (depending on how you are populating your dataset). If you are defining it yourself, you could set the default value for that particular column to DbNull.Value prior to populating the values.
If that doesn't work another popular approach would be to clone your existing dataset and then define your column as null prior to populating it with the existing data (from your current dataset) :
// Clone your existing table
DataTable clonedTable = yourExistingTable.Clone();
// Define your column type here
clonedTable.Columns["PwdHash"].DataType = typeof(System.Byte);
// Import all of your rows
foreach (var row in dt.Rows)