In C#.net, I have the following DataSource setup that I am trying to dynamically assign a WHERE clause to in the code behind...
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
     ContextTypeName="MyNameSpace.DataClasses1DataContext"
     TableName="MyTableWithADateTimeColumn" >
</asp:LinqDataSource>
The code behind looks something like this...
LinqDataSource1.Where = "MyDateColumn == DateTime(" + DateTime.Now + ")";
This gives me an error of ')' or ',' expected. I've also tried casting it inside quotation marks, as well, as without casting it as DateTime and with quotation marks...
LinqDataSource1.Where = @"MyDateColumn == """ + DateTime.Now + @""" ";
This gives me Operator '==' incompatible with operand types 'DateTime' and 'String'. I've tried several other ways, but I am obviously missing something here.
Similar code is working fine for strings.
is it this? What about this then...
LinqDataSource1.Where = "MyDateColumn == DateTime.Parse(" + DateTime.Now + ")"; 
//can't create a date from string in constructor use .Parse()...
                        I believe you need to include double quotes around the string being converted to a DateTime.
LinqDataSource1.Where = "MyDateColumn == DateTime(\"" + DateTime.Now.ToString() + "\")";
                        LinqDataSource1.Where = "MyDateColumn == Convert.ToDateTime(\"" + DateTime.Now + "\")";
It's simple and straight forward:
Look in the page source of "asp:LinqDataSource" and add that clause in the "where" section.
Adding this through the wizard with a NULL parameter fails.
Here is an example:
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="MyDataContext"
    EntityTypeName="" GroupBy="MyItem" Select="new (key as Item1, Count() as TotalQuantity)"
    TableName="MyTable" 
    Where="Country == @Country && DateProcessed == NULL">
    <WhereParameters>
        <asp:ControlParameter ControlID="ddlCountry" DefaultValue="US" 
            Name="Country" PropertyName="SelectedValue" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With