简体   繁体   English

asp.net DetailView日期时间字段格式与SQL Server格式不匹配

[英]asp.net DetailView datetime field format doesn't match the SQL Server format

I have a asp.net (WebForms) application which has a details view (insert Mode) to add new data. 我有一个asp.net(WebForms)应用程序,该应用程序具有用于添加新数据的详细信息视图(插入模式)。

2 of its fields are datetime type. 它的两个字段是datetime类型。 I have defined the SQL Server database columns as datetime . 我已将SQL Server数据库列定义为datetime

Now whenever I'm trying to add date and time it says that it doesn't match the format! 现在,每当我尝试添加日期和时间时,它都说它与格式不匹配!

I have seen people having the same problem but unfortunately their solutions didn't work for me. 我已经看到人们有同样的问题,但是不幸的是他们的解决方案对我不起作用。 How can i make sure the format matches? 如何确定格式匹配? Is it possible to display the current datetime as a sample so the user follow it? 是否可以将当前日期时间显示为示例,以便用户遵循?

Thanks in advance! 提前致谢!

DetailsView Code: 详细信息查看代码:

<Fields>
          <asp:BoundField DataField="projectName" HeaderText="projectName" SortExpression="projectName" />
          <asp:BoundField DataField="projectType" HeaderText="projectType" SortExpression="projectType" />
          <asp:BoundField DataField="subjectName" HeaderText="subjectName" SortExpression="subjectName" />
          <asp:BoundField DataField="studentID" HeaderText="studentID" SortExpression="studentID" />
          <asp:BoundField DataField="supervisorID" HeaderText="supervisorID" SortExpression="supervisorID" />
          <asp:BoundField DataField="startDate" HeaderText="startDate" SortExpression="startDate" />
          <asp:BoundField DataField="dueDate" HeaderText="dueDate" SortExpression="dueDate" />
          <asp:CommandField ShowInsertButton="True" />
</Fields>

Its better to use a standard neutral format irrespective of any Regional, Language .. settings. 最好使用标准的中性格式,而不考虑任何区域,语言..设置。

Use the following format for datetime. 将以下格式用于日期时间。 If you input your datetime in unseparated or ISO 8601 format shown below, then you should not worry about any configuration/settings: 如果您以以下所示的未分隔或ISO 8601格式输入日期时间,则无需担心任何配置/设置:

Unseparated = 'yyyymmdd hh:mm:ss'
ISO 8601    = 'yyyy-mm-ddThh:mm:ss'

The ISO 8601 format has the advantage of being defined in an international standard . ISO 8601格式具有在国际标准中定义的优点 And you can pass this format using ToString() method, when handling datetime values: 在处理日期时间值时,可以使用ToString()方法传递此格式:

lblDateTime.Text = (DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss"));

This got various advantages as it doesn't needs to check/change settings of Operating System, Database servers ... Continue reading here . 这具有各种优点,因为它不需要检查/更改操作系统,数据库服务器的设置... 在这里继续阅读

Check Your PC Date format and enter date of same format in due date if you enters the date time dynamically then use like this 如果您动态输入日期时间,请检查您的PC日期格式并在到期日输入相同格式的日期,然后像这样使用

DateTime.Now.ToString(yyyy-MM-dd)

here yyyy-MM-dd is my date format enter yours format in this and this is for current date you can use it for the date you desired 这里yyyy-MM-dd是我的日期格式,请以此格式输入您的格式,这是当前日期,您可以将其用于所需的日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM