This is a migrated thread and some comments may be shown as answers.

Linq where clause in aspx with DateTime does not work

2 Answers 97 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sucheta Patil
Top achievements
Rank 1
Sucheta Patil asked on 12 Oct 2010, 04:22 PM
I have a lstCentre in dropdown and txtOnDate as datepicker.
Based on these values the grid should display data.
I am using Linqdatasource with where clause
I tried all the following:
1. Where="Centre == @Centre && ExpectedDate == (DateTime.Parse(@ExpectedDate))"
2. Where="Centre == @Centre && (DateTime.Parse(ExpectedDate)) == @ExpectedDate"
3. Where="Centre == @Centre && ExpectedDate == Convert.ToDateTime(@ExpectedDate)"

None of them work and give me error as shown in screen shot attached.
Below is the code:
        <asp:Label ID="lbCentre" runat="server" Text="Centre:" AssociatedControlID="lstCentre"></asp:Label><telerik:RadComboBox ID="lstCentre" runat="server" AppendDataBoundItems="True" DataSourceID="LinqCentre"
            DataTextField="Centre" DataValueField="Centre" DataKeyField="Centre" DataSortField="Centre" ReadOnly="true"
            CausesValidation="False" onselectedindexchanged="lstCentre_SelectedIndexChanged" AutoPostBack="true"><Items><telerik:RadComboBoxItem Text="Choose One..." Value="10" /></Items></telerik:RadComboBox>
        <li><asp:Label ID="lbOnDate" runat="server" Text="On (Date):" AssociatedControlID="txtOnDate"></asp:Label>
            <telerik:RadDatePicker ID="txtOnDate" runat="server" DateFormat="dd/MM/yyyy" 
                Enabled="true" Culture="English (United Kingdom)" AutoPostBack="true"
 <asp:LinqDataSource ID="LinqVisitors" runat="server" 
                Where="Centre == @Centre && ExpectedDate == (DateTime.Parse(@ExpectedDate))" 
                <asp:ControlParameter ControlID="lstCentre" Name="Centre" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="txtOnDate" Name="ExpectedDate" 
                    PropertyName="SelectedDate" Type="DateTime" />
protected void LinqVisitors_Selecting(object sender, LinqDataSourceSelectEventArgs e)
           foreach (KeyValuePair<string, object> kvp in e.WhereParameters) 
               if (kvp.Value == null) 
               { e.Cancel = true; return; } 

Please help me.
Thanks in advance..

2 Answers, 1 is accepted

Sort by
Telerik team
answered on 14 Oct 2010, 10:15 AM
Hi Sucheta Patil,

DateTime.Parse(string) method takes as a parameter, string. You are trying to call it with a parameter of type DateTime. Try to set type="string", to your the second control parameter. Or modify the where clause like "ExpectedDate == @ExpectedDate".

All the best,
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Sucheta Patil
Top achievements
Rank 1
answered on 14 Oct 2010, 11:59 AM
Thanks Vasil,
Changing to String didnt help.
Earlier  I had tried using
Where="Centre == @Centre && ExpectedDate == @ExpectedDate"
Which did not work.
And now all of a sudden  it works!!!
Bit weird.But anyways,
Thanks a ton!!!
General Discussions
Asked by
Sucheta Patil
Top achievements
Rank 1
Answers by
Telerik team
Sucheta Patil
Top achievements
Rank 1
Share this question