ASP. NET solves the SQLite date type problem: the string is not recognized as a valid datetime

Source: Internet
Author: User
Tags iso 8601
Recently I used ASP. NET + SQLite database to develop a website. Program As a result, there was a headache in date processing:
Note: An error occurred while executing the current Web request. Check the stack trace information to learn about this error and Code Detailed information about the cause of the error.
Exception details: System. formatexception: the string is not recognized as a valid datetime.

Solution:

Converts a type when the date is saved to the SQLite database, for example, string _ now = system. datetime. Now. tostring ("S ");

That is to say, adding s to the. tostring () method solves the problem of date reading errors.

Simple code example:

String _ indate = request ["indate"]; // The input date, for example, 2009-2-21
Datetime _ intime = convert. todatetime (_ indate );

// The SQL statement for saving data is as follows:

Insert into table (indate) values ('"+ _ intime. tostring (" S ") +"'); // convert

Because ISO 8601 Description: (http://msdn.microsoft.com/zh-cn/library/ms187819.aspx)

A string example is provided:
* 2004-05-23t14: 25: 10
* 2004-05-23t14: 25: 10.487

Therefore, the date saved by SQLite must be converted to ISO 8601 standard string format.

Use date. tostring ("S"); this method is converted to ISO 8601 standard string format.

This article is copyrighted by it.com (www.xueit.com). This statement must be reprinted by any organization or individual.ArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

recently used ASP. net + SQLite database to develop a website program, the results encountered a headache in date Processing:
Note: An unhandled exception occurs during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.
exception details: system. formatexception: the string is not recognized as a valid datetime.

Solution:

Converts a type when the date is saved to the SQLite database, for example, string _ now = system. datetime. Now. tostring ("S ");

That is to say, adding s to the. tostring () method solves the problem of date reading errors.

Simple code example:

String _ indate = request ["indate"]; // The input date, for example, 2009-2-21
Datetime _ intime = convert. todatetime (_ indate );

// The SQL statement for saving data is as follows:

Insert into table (indate) values ('"+ _ intime. tostring (" S ") +"'); // convert

Because ISO 8601 Description: (http://msdn.microsoft.com/zh-cn/library/ms187819.aspx)

A string example is provided:
* 2004-05-23t14: 25: 10
* 2004-05-23t14: 25: 10.487

Therefore, the date saved by SQLite must be converted to ISO 8601 standard string format.

Use date. tostring ("S"); this method is converted to ISO 8601 standard string format.

This article is copyrighted by it.com (www.xueit.com). This statement must be retained by any organization or individual and the original article is clearly displayed on the article page. Otherwise, the legal liability will be held accountable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.