String was not recognized as a valid TimeSpan. Error in C# inserting time to SQL Database

I’m using this code to insert time to Sql Database
TimeSpan.Parse(string.Format(DateTime.Now.ToShortTimeString(), "hh:Mi"))

I tried to change the syntax of time "hh:mm:ss" and "hh:mm tt"

>Solution :

You should not be using any strings at all. If you want a TimeSpan containing the current time then create that:

var currentTime = new TimeSpan(DateTime.Now.Hours, DateTime.Now.Minutes, 0)

If you’re saving to a database column that is an actual time data type, that’s what you want.

Leave a Reply