SQL decimal conversion error

Posted by jakesankey on Stack Overflow See other posts from Stack Overflow or by jakesankey
Published on 2010-06-01T18:55:16Z Indexed on 2010/06/01 19:03 UTC
Read the original article Hit count: 260

Filed under:
|
|

Hey, my app parses numerous txt files in a directory that are almost all formatted identically, then inserts the values into columns of my sql db. However, there are a few files that the app has come across where a value is 'blank' instead of '0.0', so the application fails stating that it cannot convert that value to numeric.

Is there a way I could change the following code to say what says AND also add 'IF value is blank, then import 0.0 instead?

foreach (SqlParameter parameter in insertCommand.Parameters)
{
    parameter.Value = null;
}

string[] values = line.Split(new[] { ',' });

for (int i = 0; i < values.Length - 1; i++)
{
   // if (i = "" || i = null) continue;
    SqlParameter param = insertCommand.Parameters[i];
    if (param.DbType == DbType.Decimal)
    {
        decimal value;
        param.Value = decimal.TryParse(values[i], out value) ? value : 0;
    }
    else
    {
        param.Value = values[i];
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql