12

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.

6 Answers 6

12

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()...
Sign up to request clarification or add additional context in comments.

1 Comment

take a look again... there is no new DateTime(string value) constructor you need to use DateTime.Parse(string value)...
2

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

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

1 Comment

I had tried single quotes without the cast but hadn't thought to try them with the DateTime cast. Unfortunately it gives the error "Character literal must contain exactly one character"
1

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

Comments

1

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>

Comments

0

So the final solution as suggested by J.13.L looked like this...

LinqDataSource1.Where = @"MyDateColumn == DateTime.Parse(""" + MyDateTime + @""") ";

But since I didn't want to match on the time part of the date it really looked more like this...

LinqDataSource1.Where = @"MyDateColumn >= DateTime.Parse(""" + MyDateTime + @""") AND MyDateColumn < DateTime.Parse(""" + MyDateTime.AddDays(1) + @""")";

Comments

0

Another programmatically way:

dataSource.WherePredicateParameters.Clear();
OrExpressionParameter expression = new OrExpressionParameter();
expression.Parameters.Add("Birthday", DbType.DateTime, Convert.ToDateTime(txtBirthday.Text).ToString());
dataSource.WherePredicateParameters.Add(expression);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.