Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the LinqDataSource Where Clause using DateTime Column

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.

like image 849
BrianG Avatar asked Jun 26 '09 20:06

BrianG


4 Answers

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()...
like image 173
bytebender Avatar answered Oct 29 '22 22:10

bytebender


I believe you need to include double quotes around the string being converted to a DateTime.

LinqDataSource1.Where = "MyDateColumn == DateTime(\"" + DateTime.Now.ToString() + "\")";
like image 43
Reed Copsey Avatar answered Oct 29 '22 22:10

Reed Copsey


LinqDataSource1.Where = "MyDateColumn == Convert.ToDateTime(\"" + DateTime.Now + "\")";

like image 1
Venugopal Avatar answered Oct 29 '22 20:10

Venugopal


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 &amp;&amp; DateProcessed == NULL">
    <WhereParameters>
        <asp:ControlParameter ControlID="ddlCountry" DefaultValue="US" 
            Name="Country" PropertyName="SelectedValue" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>
like image 1
Sanjay Gupta Avatar answered Oct 29 '22 20:10

Sanjay Gupta