Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

No luck in creating dynamic Linq WhereParameters in OnSelecting event

Posted on 2009-05-08
7
Medium Priority
?
1,604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 39

Accepted Solution

by:
abel earned 2000 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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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