System.FormatException:字符串未被识别为有效的日期时间
我正在使用 c#.net。预先感谢您的任何帮助。
我正在使用 Repeater 和 ObjectDataSource。我使用 LINQ 连接到数据库。这需要传递一个参数(在 WHERE 子句中使用)
public IQueryable<comments> GetComments(DateTime todaysDate)
{
return (from c in dc.comments
where displayDate.Date == todayDate.Date
select c);
}
我遇到了上面的错误,但不知道为什么。问题就在这里:
<asp:Parameter DefaultValue="<%=Convert.ToDateTime(DateTime.Now)%>" Name="todayDate" Type="DateTime" />
如果我提供一个实际日期,它就可以工作。例如:
<asp:Parameter DefaultValue="02/09/2009" Name="todayDate" Type="DateTime" />
我也尝试过以下操作并收到相同的错误:
DateTime.Now.Date
Datetime.Now
Datetime.Today
Datetime.Now.ToString
Datetime.Now.Date.ToString.
我做错了什么?
谢谢
克莱尔
I am using c#.net. Thanks in advance for any help.
I am using a Repeater and a ObjectDataSource. I use LINQ to connect to the database. This requires a parameter to be passed through (used within the WHERE clause)
public IQueryable<comments> GetComments(DateTime todaysDate)
{
return (from c in dc.comments
where displayDate.Date == todayDate.Date
select c);
}
I am encounting the error above and don't know why. Here is where the problem lies:
<asp:Parameter DefaultValue="<%=Convert.ToDateTime(DateTime.Now)%>" Name="todayDate" Type="DateTime" />
If I provide a actual date it works. For example:
<asp:Parameter DefaultValue="02/09/2009" Name="todayDate" Type="DateTime" />
I have also tried the following and recieved the same error:
DateTime.Now.Date
Datetime.Now
Datetime.Today
Datetime.Now.ToString
Datetime.Now.Date.ToString.
What am I doing wrong?
Thanks
Clare
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 <%= .. %>服务器控件中的语法 () 是不可能的。使用代码隐藏来设置属性。
Using <%= .. %> syntax in a server control () is not possible. Use code-behind to set the property.
您可以在页面加载中添加SelectParameter。只需添加此内容 -
编辑:感谢汉斯的更正。
You can add the SelectParameter in the page load. Just add this -
Edit: Thanks Hans for the correction.
如果您复制并粘贴了代码,则函数中可能存在拼写错误 - 函数参数名为“todaysDate”,但 where 语句使用“todayDate”(这是您的 ASP 参数)。
如果情况并非如此,请发布您调用 GetComments 函数的位置。
If you've copied and pasted your code, then you might have a typo in the function - the function parameter is named todaysDate, but the where statement uses todayDate (which is your ASP parameter).
If this is not the case, please post where you call your GetComments function from.
您确定这是错误的正确位置吗? Convert.ToDateTime 的作用如下:
DateTime 是一个 IConvertible,它非常简单地实现 ToDateTime:
正如 Chris 指出的那样,没有理由将 DateTime.Now 转换为 DateTime。已经是一个了。
Are you sure this is the correct location of the error? Here is what Convert.ToDateTime does:
DateTime is an IConvertible, and it implements ToDateTime very simply:
As Chris pointed out, there is no reason to convert DateTime.Now to a DateTime. It already is one.
谢谢大家的帮助。你让我走上了正轨。
发现我可以在后面的代码中设置 DefaultValue 后,我再次查看网络并发现 本教程。
现在这正在发挥作用。
这是我的代码:
但是请注意,首先您必须创建一个“选择”事件(在属性选项卡中)。
我希望这是正确的做法。有人对此有何评论吗?
再次感谢
克莱尔
Thank you everyone for you help. You put me on the right track.
After finding out I could set the DefaultValue within the code behind I have another look around the web and found this tutorial.
This is now working.
Here is my code:
However please note first you must create a 'Selecting' event (within the properties tab).
I hope this is the correct way of doing it. Does anyone have any comments on this?
Thanks again
Clare