Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assign a DBNull in a better way?

I need to parse a value from a DataRow and assign it to another DataRow. If the input is valid, then I need to parse it to a double, or else add a DBNull value to the output. I'm using the following code:

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;
}

Is there a better way to do it?

like image 954
Mike Avatar asked Feb 17 '11 07:02

Mike


1 Answers

How about:

    public double? GetVolume(object data)
    {
        double value;
        if (data != null && double.TryParse(data.ToString(), out value))
            return value;
        return null;
    }

    public void Assign(DataRow theRowInput, DataRow theRowOutput)
    {
        theRowOutput[0] = (object)GetVolume(theRowInput[0]) ?? DBNull.Value;
    }
like image 200
Marc Gravell Avatar answered Sep 19 '22 08:09

Marc Gravell