SHeinema
SHeinema

Reputation: 634

ASP.NET EntityDataSource WHERE Clause

I am new to ASP.NET and could use some help writing a where clause for my EntityDataSource.

I have the following EDS:

<asp:EntityDataSource ID="RidesEDS" runat="server" 
    ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False" 
    EntitySetName="Rides" EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>

There is a database of 'Rides' and a database of 'AdminOptions' which contains two dates: validDate1 and validDate2... I need the EDS to only show rides that have 'CallTime's between the two valid dates. To facilitate this, on page_load I am populating two hidden fields with the valid dates (hf_validDate1 and hf_validDate2). Can anyone show me what I need to add to the EntityDataSource code to accomplish this by comparing CallTimes to the values of the hf's with a WHERE clause?

EDIT:

Here is what I have so far, not quite working though..

<asp:EntityDataSource ID="RidesEDS" runat="server" 
        ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False" 
        EntitySetName="Rides" EnableDelete="True" EnableUpdate="True" Where="it.TimeOfCall > @validDate1Param AND it.TimeOfCall < @validDate2Param">

        <WhereParameters>
        <asp:ControlParameter ControlID="hf_validDate1" DbType="DateTime" 
          DefaultValue="1/01/2012 12:00:00 PM" Name="validDate1Param" PropertyName="Value" />
          <asp:ControlParameter ControlID="hf_validDate2" DbType="DateTime" 
          DefaultValue="1/01/2112 12:00:00 PM" Name="validDate2Param" PropertyName="Value" />
        </WhereParameters>
    </asp:EntityDataSource>

<asp:HiddenField ID="hf_validDate1" runat="server" />
<asp:HiddenField ID="hf_validDate2" runat="server" />

Code-Behind:

protected void Page_Load(object sender, EventArgs e)
        {
            using(RamRideOpsEntities myEntities = new RamRideOpsEntities())
            {
                var validDates = (from a in myEntities.AdminOptions
                                  select new { a.ValidDate1, a.ValidDate2 }).FirstOrDefault();

                if(validDates != null)
                {
                    hf_validDate1.Value = validDates.ValidDate1.ToString();
                    hf_validDate1.Value = validDates.ValidDate2.ToString();
                }
            }            
        }

Upvotes: 1

Views: 10389

Answers (2)

Eric
Eric

Reputation: 2283

Just looking at the answer that Ravi posted, what worked for me was doing it in code behind like this:

RidesEDS.WhereParameters.Add("CategoryID", TypeCode.String, hiddenField.Value);

And then setting this in the server tag of the EntityDataSource:

AutoGenerateWhereClause="true"

The only thing I'd add is if you do it in codebehind, make sure you have a (!PostBack) check before you add the parameter. I was doing it on postback and started getting crazy results.

Upvotes: 0

Ravi Gadag
Ravi Gadag

Reputation: 15861

you have to use Where Parameters, in the entiry datasource declaration. you can check this link, which has basic tutorials to understand the things. Entity Datasource filtering

like this

<asp:EntityDataSource ID="RidesEDS" runat="server" 
    ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False" 
    EntitySetName="Rides" EnableDelete="True" EnableUpdate="True">

// this needs to be added 
<WhereParameters> 
        <asp:ControlParameter ControlID="yourHiddenFiledID" DbType="YourHiddenFieldDataType" 
          DefaultValue="SomeDefaultValue" Name="NameToDescribe" PropertyName="Text" />
      </WhereParameters>

</asp:EntityDataSource>

if you want to add programatticaly, then you can do like this

RidesEDS.WhereParameters.Add("CategoryID", TypeCode.String, hiddenField.Value);

Upvotes: 3

Related Questions