Link to home
Start Free TrialLog in
Avatar of Ken Fayal
Ken FayalFlag for United States of America

asked on

No luck in creating dynamic Linq WhereParameters in OnSelecting event

Hello,

I am trying to create a dynamic where parameter for my linq data source which is used by my GridView control.  My OnSelecting code is as follows:

When I step through I see that my variable _policyID has a value, but I receive an error when the page loads:

No property or field 'pid' exists in type 'M_Payment'

I've attached the code below

Am I missing something?  Is this even possible to do?
// Here is my code 
protected void LinqPaymentsDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    Parameter whereParam = new Parameter();
    whereParam.Name = "pid";
    whereParam.DefaultValue = _policyID.ToString();
    whereParam.Type = TypeCode.Int32;
    LinqPaymentsDS.WhereParameters.Add(whereParam);
    LinqPaymentsDS.Where = "LOC_Policy_Detail_ID == @pid";
} 
 
// Here is the declared portion on my .ASPX page
<asp:LinqDataSource ID="LinqPaymentsDS" runat="server" OnSelecting="LinqPaymentsDS_Selecting" ContextTypeName="ABC_SystemTracking_LINQ.ABC_SystemTracking_DataClassesDataContext" EnableUpdate="true" EnableUpdate="true" TableName="M_Payments" OrderBy="Coverage_Date descending">
</asp:LinqDataSource>

Open in new window

Avatar of abel
abel
Flag of Netherlands image

This may not be of too much help yet, but have you tried the shortcut:

LinqPaymentsDS.WhereParameters.Add("pid", TypeCode.Int32, "20");
(it shouldn't make any difference of course, but just wondering)

btw: did you know you have twice EnableUpdate="true" in the LinqDataSource declaration?
looking at it again, there are some small things that bug me with your code:

1. the "==" really should be an "=" (it's query syntax, not C# syntax)

2. (not sure but) it seems to me that you should actually use e.WhereParameters here, and not the LinquPaymentsDS.WhereParameters. You are in the middle of the event, use the object that is send to that event for changing any where-values on-the-fly.


Avatar of Ken Fayal

ASKER

abel,
Thanks for the response.

I have seen and tried both = and == operators.  Changing it doesn't make a difference.  Also, I cannot use the e object because it does not include a .Where property.  I can only access it by directly addressing the LinqPaymentsDS object.  
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
abel,
Thanks for this.   My solution ended up being used from the exact article you listed.  You get all the points.


// My code-behind code...
 
protected void LinqPaymentsDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.WhereParameters.Add("LOC_Policy_Detail_ID", _policyID);
}
 
// My LinqPaymentsDS declaration in the .aspx page...
<asp:LinqDataSource ID="LinqPaymentsDS" runat="server" OnSelecting="LinqPaymentsDS_Selecting" ContextTypeName="ABC_SystemTracking_LINQ.ABC_SystemTracking_DataClassesDataContext" EnableUpdate="true" EnableUpdate="true" TableName="M_Payments" OrderBy="Coverage_Date descending" AutoGenerateWhereClause="true">
</asp:LinqDataSource>

Open in new window

You've helped me again!
great it worked! And nice to see that the lines of code actually just gotten smaller and clearer :)