Solved

No luck in creating dynamic Linq WhereParameters in OnSelecting event

Posted on 2009-05-08
7
1,589 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Accepted Solution

by:
abel earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
You've helped me again!
0
 
LVL 39

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now