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 134 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
Hi
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:
Visitors.aspx
******************************************************************
<li>
        <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><li></li>
        <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"
                onselecteddatechanged="txtOnDate_SelectedDateChanged"></telerik:RadDatePicker>
        </li>
 <asp:LinqDataSource ID="LinqVisitors" runat="server" 
            ContextTypeName="CWC.Staffnet.Forms.StaffnetDataContext" 
            TableName="tblVisitors"  
                Where="Centre == @Centre && ExpectedDate == (DateTime.Parse(@ExpectedDate))" 
                onselecting="LinqVisitors_Selecting">
            <WhereParameters>
                <asp:ControlParameter ControlID="lstCentre" Name="Centre" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="txtOnDate" Name="ExpectedDate" 
                    PropertyName="SelectedDate" Type="DateTime" />
            </WhereParameters>
        </asp:LinqDataSource
*****************************************************
Visitors.aspx.cs
*****************************************************
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..
S

2 Answers, 1 is accepted

Sort by
0
Accepted
Vasil
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,
Vasil
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
0
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!!!
S
Tags
General Discussions
Asked by
Sucheta Patil
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Sucheta Patil
Top achievements
Rank 1
Share this question
or