Ken Fayal
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?
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>
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.WhereParam eters. 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.
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.WhereParam
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
abel,
Thanks for this. My solution ended up being used from the exact article you listed. You get all the points.
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>
ASKER
You've helped me again!
great it worked! And nice to see that the lines of code actually just gotten smaller and clearer :)
btw: did you know you have twice EnableUpdate="true" in the LinqDataSource declaration?