Skip to content
Advertisement

Failed to convert from a String to a TimeSpan error

I am attempting to post to my Database using a SqlCommand like so

 queryLogResults.Parameters.Add("@executionTime", SqlDbType.Time).Value = executionTime;

This is the string I am passing in as executionTime

string performanceTime = _stopWatch.Elapsed.ToString("mm':'ss':'fff");

I am assuming it has something to do with the milleseconds because it works only sometimes, and if I post just minutes and seconds it works every time. Am I not using the right type of SqlDbType? In my Database ExecutionTime is a time(7) value.

Advertisement

Answer

If you’re trying to set a parameter value that’s of type SqlDbType.Time, then you shouldn’t need to convert it to a string. Just use

TimeSpan performanceTime = _stopWatch.Elapsed;
queryLogResults.Parameters.Add("@executionTime", SqlDbType.Time).Value = performanceTime;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement