I am trying to validate types when updating a DataRow. I am using the following code:
public static int Write<T>(T data, DataRow row, string FieldName)
{
if (null == data)
{
row[FieldName] = row.Table.Columns[FieldName].DefaultValue;
}
else
{
if (row.Table.Columns[FieldName].DataType is T)
{
row[FieldName] = data;
return 0;
}
else
{
try
{
row[FieldName] = (row.Table.Columns[FieldName].DataType)Convert.ChangeType(data, row.Table.Columns[FieldName].DataType);
}
catch (InvalidCastException)
{
return 1;
}
}
}
return 0;
}
But it's not working. On the if row.Table.Columns[FieldName].DataType is T
I compared a GUID column with a GUID and returned false.
The part (row.Table.Columns[FieldName].DataType)Convert.ChangeType(data, row.Table.Columns[FieldName].DataType);
I have a syntax error, the (row.Table.Columns[FieldName].DataType)
is not working.
I am open to another type of validation.