Solved

No luck in creating dynamic Linq WhereParameters in OnSelecting event

Posted on 2009-05-08
7
1,591 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
DevOps Toolchain Recommendations

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

 
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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

23 Experts available now in Live!

Get 1:1 Help Now