Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

No luck in creating dynamic Linq WhereParameters in OnSelecting event

Posted on 2009-05-08
7
Medium Priority
?
1,609 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

879 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