LinqDataSource and DateTime format

I am trying to create a LinqDataSource to bind to a DropDownList in an ASP.NET form. I want to show only items according to date (which is one of the fields in the database).

Basically, the elements that I want to show are those that will be executed in futures (i.e. after DateTime.Now).

I tried the following markup:

<asp:DropDownList runat="server" ID="DropDownList1" 
    AppendDataBoundItems="True" DataSourceID="LinqDataSource1"
    DataTextField="TextField" DataValueField="ValueField">
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="DataContext1" TableName="Table" 
    Where="DateField &gt;= @DateField">
    <WhereParameters>
        <asp:Parameter DefaultValue="DateTime.Now" Name="DateField" 
            Type="DateTime" />
    </WhereParameters>
</asp:LinqDataSource>

I get an exception in the format saying "the string was not recognized as a valid DateTime" when I try to run it. However, the dates in my database seem fine, because DateTime.Parse works great on them. DateField is a datetime type in SQL.

What am I missing here?

Thanks!

+3
source share
3

DefaultValue , .

, DefaultValue

 "<%# DateTime.Now %>"

Andomar ( :

<WhereParameters>
                <asp:Parameter DefaultValue="<%# DateTime.Now %>" Name="DateField" Type="DateTime" />
</WhereParameters>

, DataBinding , DataBinding, Parameter ControlParameter.

TextBox Label <% #% > ( ), DateTime, SQL DateTime .NET DateTime .

Page_Load,

DataContext DataContext1 = new DataContext();

    var c = from a in DataContext1.Field
            where a.DateField >= DateTime.Now
            select a;
    DropDownList.DataSource = c;
    DropDownList.DataBind();
+3

, DefaultValue.

+1

Try:

DefaultValue="<%# DateTime.Now %>"
+1

Source: https://habr.com/ru/post/1709192/


All Articles