In my app, I have the following line:
double val = Convert.ToDouble(values8[x]) + 24837;
If values8[x]
can't be converted to a Double
the app crashes with the error Input string was not in a correct format
.
How do I test this, and set val
to DBNull.Value
if the conversion fails? With a try/catch? Would something like this be acceptable?
EDIT: Here's what I'm trying to do
try
{
double val = Convert.ToDouble(values8[x]) + 24837;
com.Parameters.Add("@p1", OleDbType.Date, 255).Value = DateTime.FromOADate(val);
}
catch (exception e)
{
com.Parameters.Add("@p1", OleDbType.Date, 255).Value = DBNull.Value;
}
EDIT2: TryParse
is what I was looking for.