System.FormatException:该字符串未被识别为有效的DateTime [英] System.FormatException: String was not recognized as a valid DateTime

查看:1929
本文介绍了System.FormatException:该字符串未被识别为有效的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#.NET。感谢您事先的任何帮助。

I am using c#.net. Thanks in advance for any help.

我使用一个中继器和一个ObjectDataSource控件。我使用LINQ连接到数据库。这需要一个参数,通过传递(WHERE子句中使用)

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);
    }

我encounting上面的错误,不知道为什么。这是问题所在:

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?

感谢

克莱尔

推荐答案

感谢大家对你有所帮助。你把我在正确的轨道。

Thank you everyone for you help. You put me on the right track.

查不到我可以内code设置默认值后面我有各地的网络再看看,发现的本教程

After finding out I could set the DefaultValue within the code behind I have another look around the web and found this tutorial.

这是现在的工作。

下面是我的code:

    protected void comments_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["todayDate"] = DateTime.Now;
    }

但是请注意首先你必须创建一个选择事件(在属性选项卡内)。

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?

再次感谢

克莱尔

这篇关于System.FormatException:该字符串未被识别为有效的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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