How can I assign a DBNull in a better way?
Posted
by
Mike
on Stack Overflow
See other posts from Stack Overflow
or by Mike
Published on 2011-02-17T07:07:19Z
Indexed on
2011/02/17
7:25 UTC
Read the original article
Hit count: 150
Hi,
I need to parse value from a datarow and assign it to another datarow.If the input is valid, then I need to parse it to double or else add a dbnull value to the output.I'm doing the following, is there a better way to do it?
public double? GetVolume(object data)
{
string colValue = data == null ? string.Empty : data.ToString();
double volume;
if (!Double.TryParse(colValue.ToString(), out volume))
{
return null;
}
return volume;
}
public void Assign(DataRow theRowInput,DataRow theRowOutput)
{
double? volume = GetVolume(theRowInput[0]);
if(volumne.HasValue)
theRowOutput[0] = volume.value;
else
theRowOutput[0] = DbNull.Value;
return theRowOutput;
}
Thanks, -M
© Stack Overflow or respective owner