日期时间转换错误 [英] error with date time converting

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

问题描述

我想更新数据库表中的日期值.这是类型日期字段.
在我的表单中,我使用一个名为dtpRecallDate的日期时间选择器选择日期,并使用另一个名为dtpRecallTime的选择器来选择时间.
我正在使用的代码如下:

HI there, i want to update the date value in my db table. it is a type date field.
In my form I use a datetime picker called dtpRecallDate for choosing the date and another one called dtpRecallTime for choosing the time.
The code i am using is the following:

DateTime dtime;
string RecallDateTime = "";

if (dtpRecallDate.Value.ToString() != "" && dtpRecallTime.Value.ToString() != "")
{
    RecallDateTime = (dtpRecallDate.Value.ToString("s") + " " + dtpRecallTime.Value.ToString("s"));

}
DateTime.TryParse(RecallDateTime, out dtime);
MerchantData1["RecallDate"] = dtime.ToString();
DataConnection.UpdateRow("MerchantCalls", MerchantData1, " WHERE  MerchantCallID = " + _merchcallid + " ");



当我按下回车按钮时,我收到以下消息:

从字符串转换日期时间的语法错误"

我知道这可能很简单,但是我已经花了一个小时试图解决它! 任何帮助将不胜感激.
提前thnx!



When i press the enter button i get the following message:

"syntax error converting datetime from character string"

I know it''s probably something very simple but i have already waisted one hour trying to solve it!!!
Any help will be much appreciated.
Thnx in advance!

推荐答案

阅读代码.使用调试器逐步进行调试.了解如何使用您的工具.

如果两个值相同,则RecallDateTime为".否则,它将两个日期合并在一起.它永远不会是一个日期,因此解析将永远无法进行.同样,当您调用tryparse时,它返回true或false,如果忽略该返回值,则说明您编写的是错误的代码.系统告诉您无法解析该字符串,因此您将其忽略.
Read your code. Use the debugger to step through it. Learn how to use your tools.

RecallDateTime is "" if the two values are the same. Otherwise, it''s two dates joined together. It''s never one date, so parsing will never work. Also, when you call tryparse, it returns true or false, if you ignore that return value, you''re writing bad code. The system told you it could not parse the string and you ignored it.


如果使用断点并在此处检查以下代码,
If you use breakpoint and check the following code here,
RecallDateTime = (dtpRecallDate.Value.ToString("s") + " " + dtpRecallTime.Value.ToString("s"));


时间为9秒时,RecallDateTime的值将为9 9.或说
23 59,当dtpRecallDate中的秒为23秒,而dtpRecallTime中的时间为59秒时.

这不是有效的日期时间格式.


RecallDateTime will have value of say 9 9 when the time is 9 seconds. or say
23 59 when seconds in dtpRecallDate is 23 seconds, and time in dtpRecallTime is 59 seconds.

This is not a valid datetime format.


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

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