日期时间格式错误 [英] DateTime Format error

查看:114
本文介绍了日期时间格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将日期时间从用户控件转换为日期时间变量,以将其发送到数据库,并出现错误,因为输入字符串的日期时间格式不正确"
请检查下面的代码,

condObj.DiagnosisDate = Convert.ToDateTime(txtDateTimeDiag.ToString()); 其中txtDateTimeDiag是我的用户控件.

感谢您对转换的任何帮助.

该代码不会超出我尝试转换日期的行.
日期以 2011-12-06 11:56:52.777

I am trying to convert Date Time from a User Control to a Date Time variable to send it to the Database and getting the error as " Input String was not in a correct date time format "
Please check the code below,

condObj.DiagnosisDate = Convert.ToDateTime(txtDateTimeDiag.ToString()); where txtDateTimeDiag is my user control.

Any help on conversion is appreciated.

The code is not going beyond the line where I am trying to convert the date.
The date is stored in the database as 2011-12-06 11:56:52.777

推荐答案

存储在数据库中,也可以添加到以前的解决方案中,尝试使用 DateTime.TryParse [
Also to add to previous solution try using DateTime.TryParse[^] instead. In case the text field contains illegal format you can catch the error more easily.

Also remember to use parameters in the SQL statement where you save the date unless you already do so.


我明白了.

您忘记了文本框的.Text属性.请执行以下操作:
I see.

You forgot the .Text property of the textbox. Do this:
condObj.DiagnosisDate = Convert.ToDateTime(txtDateTimeDiag.Text.ToString());



如果这解决了您的问题,请标记为答案并投票5

问候,
爱德华(Eduard)



Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard


在您自己的UserControl中,将从用户选择的日期和时间存储在DateTime结构中.
当您需要更新SQL时,请调用
DateTime.ToString("s") [ ^ ]方法.
Sortable("s")格式说明符将在SQL INSERT/UPDATE语句中工作.

更好的方法是使用SqlParamenter,这种方式将数据作为DateTime总是正确的.
http://www.dotnetperls.com/sqlparameter [
In your own UserControl, store the selected Date and Time from the user in a DateTime struct.

When you need to update SQL then call DateTime.ToString("s")[^] method.
The Sortable ("s") Format Specifier will work in a SQL INSERT/UPDATE statement.

A better way is to use SqlParamenter, this way data as DateTime will always be correct.
http://www.dotnetperls.com/sqlparameter[^]


这篇关于日期时间格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆