Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading