1

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

4

5 回答 5

2

在服务器控件 () 中使用 <%= .. %> 语法是不可能的。使用代码隐藏设置属性。

于 2009-09-02T15:16:54.113 回答
1

您可以在页面加载中添加 SelectParameter。只需添加这个 -

SqlDataSource1.SelectParameters["todayDate"].DefaultValue = Datetime.Now;

编辑:感谢汉斯的更正。

于 2009-09-02T14:50:48.353 回答
0

谢谢大家的帮助。你让我走上正轨。

在发现我可以在后面的代码中设置 DefaultValue 之后,我又在网上浏览了一下,找到了这个教程

现在正在工作。

这是我的代码:

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

但是请注意,首先您必须创建一个“选择”事件(在属性选项卡中)。

我希望这是正确的做法。有人对此有何评论吗?

再次感谢

克莱尔

于 2009-09-03T08:05:00.277 回答
0

如果您复制并粘贴了代码,那么您可能在函数中出现拼写错误 - 函数参数命名为 today s Date,但 where 语句使用 todayDate(这是您的 ASP 参数)。

如果不是这种情况,请发布您调用 GetComments 函数的位置。

于 2009-09-02T14:52:59.613 回答
0

您确定这是错误的正确位置吗?这是 Convert.ToDateTime 所做的:

    public static DateTime ToDateTime(bool value)
    {
        return ((IConvertible) value).ToDateTime(null);
    }

DateTime 是一个 IConvertible,它非常简单地实现了 ToDateTime:

    DateTime IConvertible.ToDateTime(IFormatProvider provider)
    {
        return this;
    }

正如 Chris 所指出的,没有理由将 DateTime.Now 转换为 DateTime。它已经是一个。

于 2009-09-02T14:53:30.517 回答