我面临的问题字符串未被识别为召集到日期时的有效日期时间 [英] I am facing issue string was not recognized as a valid date time when convetring to date time

查看:61
本文介绍了我面临的问题字符串未被识别为召集到日期时的有效日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i正在处理网络应用程序我在Web表单上遇到问题时单击链接按钮时会提示错误字符串在转换为日期时未被识别为有效日期时间虽然在本地机器上工作正常,但在服务器上部署后显示错误。



我尝试过:



 受保护  void  lnkbtnAddExisting_Click( object  sender,EventArgs e)
{
DevExpress.Web.GridViewDataItemTemplateContainer gvRow =((DevExpress.Web.GridViewDataItemTemplateContainer)((LinkBut​​ton)发送者).NamingContainer);
string SchemeNameID = DataBinder.Eval(gvRow.DataItem, SchemeNameID)的ToString();
DateTime StartDate = Convert.ToDateTime(DataBinder.Eval(gvRow.DataItem, StartDate ));
DateTime EndDate = Convert.ToDateTime(DataBinder.Eval(gvRow.DataItem, EndDate ));


Response.Redirect( abc.aspx?StartDate = + StartDate + & EndDate = + EndDate + & SchemeNameID = + SchemeNameID);
}

解决方案

请检查您的数据库:错误信息非常明确。

<转换为日期时间时,字符串未被识别为有效的日期时间



表示来自数据库的值不是有效日期生产机器的当前区域设置中的时间。



解决方案是更改数据库,以便将值存储在适当的数据类型中:DATE或DATETIME中的日期,INT中的整数,等等。当你将日期存储为字符串时,这只是它给你的问题的开始。


创建临时变量以查看你想用调试器转换的内容:

  string  Temp = DataBinder.Eval(gvRow.DataItem,  EndDate); 





你应该学会使用调试器尽快。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



[更新]

你确定你需要在C#中转换为DateTime吗? />
因为你有一个字符串转换为DateTime来构建一个字符串。


hi,
i am working on web application i am facing an issue on web form when click on link button its prompt an error "string was not recognized as a valid date time when converting to date time" although that on local machine working fine but after deployment on server its shows above error.

What I have tried:

protected void lnkbtnAddExisting_Click(object sender, EventArgs e)
       {
           DevExpress.Web.GridViewDataItemTemplateContainer gvRow = ((DevExpress.Web.GridViewDataItemTemplateContainer)((LinkButton)sender).NamingContainer);
           string SchemeNameID = DataBinder.Eval(gvRow.DataItem, "SchemeNameID").ToString();
           DateTime StartDate =Convert.ToDateTime( DataBinder.Eval(gvRow.DataItem, "StartDate"));
           DateTime EndDate =Convert.ToDateTime( DataBinder.Eval(gvRow.DataItem, "EndDate"));


           Response.Redirect("abc.aspx?StartDate=" + StartDate + "&EndDate=" + EndDate + "&SchemeNameID=" + SchemeNameID);
       }

解决方案

So check your DB: the error message is pretty explicit.

string was not recognized as a valid date time when converting to date time


Means that the value that came from your database is not a valid date time in the current locale for the production machine.

The solution for this is to change your DB so that you store values in appropriate data types: Dates in DATE or DATETIME, integers in INT, and so on. When you store dates as strings, this is just the beginning of the problems it will give you.


Create temp variables to see what you want to convert with the debugger:

string Temp = DataBinder.Eval(gvRow.DataItem, "EndDate");



You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

[Update]
Are you sure you need to convert to DateTime in C# ?
Because you have a string converted to DateTime to build a string.


这篇关于我面临的问题字符串未被识别为召集到日期时的有效日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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