Solved

No luck in creating dynamic Linq WhereParameters in OnSelecting event

Posted on 2009-05-08
7
1,597 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Ken Fayal
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24339386
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?
0
 
LVL 39

Expert Comment

by:abel
ID: 24339466
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.


0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24339851
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.  
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24340007
There is indeed no Where property, the Where-statement should be set previously to this event. Then you can use the SelectParameters and WhereParameters to change these values.

I think this note from MS will help you out to keep it dynamic: the AutoGenerateWhereClause is what you are after:

When you add parameters through the WhereParameters collection of the LinqDataSourceSelectEventArgs object, you must either include a placeholder for the parameter in the Where property or set the AutoGenerateWhereClause property to true. For example, if you add a parameter named ProductName in the event handler for the Selecting event, you must either set the AutoGenerateWhereClause property to true or set the Where property to ProductName=@ProductName.                
quote from: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourceselecteventargs.whereparameters.aspx
0
 
LVL 9

Author Comment

by:Ken Fayal
ID: 24340867
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

0
 
LVL 9

Author Closing Comment

by:Ken Fayal
ID: 31579578
You've helped me again!
0
 
LVL 39

Expert Comment

by:abel
ID: 24340884
great it worked! And nice to see that the lines of code actually just gotten smaller and clearer :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What are the simplest ways to create a Merge Sort? 3 34
How to read text with RegEx... 2 34
Get month and date in a format 4 44
VB.net/VSTO Excel Add-in 2 15
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question