above was just freeform ad hoc example... not typed in visual studio so ignore issues like case sensitivity. I get this error from code like that.. why? I'm finding my first experience with linq very negative. I realize I don't know what I'm doing yet,
but I'm getting very discouraged... I just need to build a pretty simple search page. My aspx markup contains the linqdatasource with everything setup except the 'where' property, which obviously I need to control at runtime based on user input on the search
page. I do have a way of making my page work using the linqdatasource's 'selecting' event but I just don't like that, it seems so out of the way for what should be so simple and straight forward. Change where condition, databind, change where condition, databind,
change where condition, databind.. why is it not that simple?
so I changed my code to like that above and I get the right results returned and bound to the gridview... BUT when I try to page or sort the where property gets ignored? now what? btw, that code that changes the where property
runs from a selectedindexchanged event of a dropdownlistbox. I want to have several user controls on the page and simply change the where property accordingly... like from selectedindexchanged or a button click in some cases.
You could have used a ControlParameter pointing to the DropDownList and you would haven't had to write any codebehind, in the first place.
Now, if you were to customize and supply the Where property in the codebehind then you're binding the GridView on your own, and you'll have to do it every time the source needs to provide data (on paging. sorting).
So, if you do the same - as in
1. provide the Where and
2. set the DataSourceID followed with the DataBind
in the PageIndexChanging and Sorting events, it will work.
1. as for the ControlParameter, I only used the dropdownlistbox as a sample, my search page will need to accomodate several different user input controls including dropdowns and textboxes. Not that I have done a ton of apps or anything, but I've never encountered
a scenario where a controlParameter would be of any use... but honestly, I really have not done a lot of apps, so don't take that the wrong way... I realize and fully admit I am not a really experienced .net programmer. I really need a straight forward flexible
way to simply customize the where clause on the fly, taking into account several different user input controls.... not necessarilly all 'AND'd' together.
2. could you elaborate a little more on your example of what would need to be done to make it work? with paging and sorting.... I follow what you mean at a conceptual level.... but that's about it.
thanks again for the response, you really are a great help. You have assisted me successfully several times before :)
thanks PeteNet. So it seems using the selecting event of the linqdatasource keeps paging and sorting working automatically.... so i will definitely stick with that event then. As for the where property, based on your example, I should be able to just dynamically
build that where property 'string' based on the state of the page (the various user input controls), right? Further, I should be able to build it using both AND and OR logical operators, grouping with (), etc, to make complex where statements... yes?
thanks PeteNet! One last follow up, which I forgot to include in my last reply... can I do this *without* needing to use whereParameters? (dynamically build a sting to be used for where property without using that WhereParameters collection and putting @placeHolders
in the query string). I don't remember why but I think I encountered some issues with dealing with whereparameters on the fly...
I'll be marking your responses as answers... as usual, they are :)
I also posted another question a few minutes ago related to the overall design of this search page I'm working on... this where property question is one of two ways I will wind up using to handle dynamically building my where conditions... the other is using
the PredicateBuilder class. So, with your help, I now know I have two options for that part... now my last problem seems to be a more high level page design issue, which is what I just added another post on.... my linqdatasource selecting event seems to be
becoming a monolithic procedure encompassing all my page logic, and I assume that probably is not right :/
can I do this *without* needing to use whereParameters? (dynamically build a sting to be used for where property without using that WhereParameters collection and putting @placeHolders in the query string).
my linqdatasource selecting event seems to be becoming a monolithic procedure encompassing all my page logic
:) inadvertently so might seem, but the other way would have been to do it without the LDS, as you have mentioned, and that too would have a single 'entry' point, so to speak. (although right now your implementation does seem to be well matched to your requirements
- maybe that's what you should think about?)
c0pe
Member
319 Points
435 Posts
linqdatasource.where property troubles
Sep 18, 2010 11:41 PM|LINK
hello,
Operator '=' incompatible with operand types 'String' and 'Int32'
linqdatasource1.where = "myFieldname = " + AlistBox.SelectedValue;
AGridView.DatasourceID = linqdatasource1;
AGridView.Databind();
above was just freeform ad hoc example... not typed in visual studio so ignore issues like case sensitivity. I get this error from code like that.. why? I'm finding my first experience with linq very negative. I realize I don't know what I'm doing yet, but I'm getting very discouraged... I just need to build a pretty simple search page. My aspx markup contains the linqdatasource with everything setup except the 'where' property, which obviously I need to control at runtime based on user input on the search page. I do have a way of making my page work using the linqdatasource's 'selecting' event but I just don't like that, it seems so out of the way for what should be so simple and straight forward. Change where condition, databind, change where condition, databind, change where condition, databind.. why is it not that simple?
edit:
<div style="position: absolute; left: -10000px; top: 34px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">ldsMain.Where = "JobNID = " + "\"" + lstJobNumbers.SelectedValue + "\"";</div> <div style="position: absolute; left: -10000px; top: 34px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> GridViewInventory.DataSourceID = "ldsMain";</div> <div style="position: absolute; left: -10000px; top: 34px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> GridViewInventory.DataBind();</div>
ldsMain.Where = "JobNID = " + "\"" + lstJobNumbers.SelectedValue + "\"";
GridViewInventory.DataSourceID = "ldsMain";
GridViewInventory.DataBind();
so I changed my code to like that above and I get the right results returned and bound to the gridview... BUT when I try to page or sort the where property gets ignored? now what? btw, that code that changes the where property runs from a selectedindexchanged event of a dropdownlistbox. I want to have several user controls on the page and simply change the where property accordingly... like from selectedindexchanged or a button click in some cases.
PeteNet
All-Star
81342 Points
11398 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 12:58 AM|LINK
You could have used a ControlParameter pointing to the DropDownList and you would haven't had to write any codebehind, in the first place.
Now, if you were to customize and supply the Where property in the codebehind then you're binding the GridView on your own, and you'll have to do it every time the source needs to provide data (on paging. sorting).
So, if you do the same - as in
1. provide the Where and
2. set the DataSourceID followed with the DataBind
in the PageIndexChanging and Sorting events, it will work.
Peter
c0pe
Member
319 Points
435 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 01:41 AM|LINK
great to hear from you PeteNet,
1. as for the ControlParameter, I only used the dropdownlistbox as a sample, my search page will need to accomodate several different user input controls including dropdowns and textboxes. Not that I have done a ton of apps or anything, but I've never encountered a scenario where a controlParameter would be of any use... but honestly, I really have not done a lot of apps, so don't take that the wrong way... I realize and fully admit I am not a really experienced .net programmer. I really need a straight forward flexible way to simply customize the where clause on the fly, taking into account several different user input controls.... not necessarilly all 'AND'd' together.
2. could you elaborate a little more on your example of what would need to be done to make it work? with paging and sorting.... I follow what you mean at a conceptual level.... but that's about it.
thanks again for the response, you really are a great help. You have assisted me successfully several times before :)
PeteNet
All-Star
81342 Points
11398 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 01:56 AM|LINK
sure, PageIndexChanging:
Peter
PeteNet
All-Star
81342 Points
11398 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 02:17 AM|LINK
one better:
you need only place the code (with some checks) in the Selecting event (and not needed in the PageIndexChanging and Sorting events):
Peter
c0pe
Member
319 Points
435 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 02:31 PM|LINK
thanks PeteNet. So it seems using the selecting event of the linqdatasource keeps paging and sorting working automatically.... so i will definitely stick with that event then. As for the where property, based on your example, I should be able to just dynamically build that where property 'string' based on the state of the page (the various user input controls), right? Further, I should be able to build it using both AND and OR logical operators, grouping with (), etc, to make complex where statements... yes?
PeteNet
All-Star
81342 Points
11398 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 03:08 PM|LINK
yes
yes. and keep this handy: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.where.aspx
* the operators would be applied as && or ||
Peter
c0pe
Member
319 Points
435 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 03:35 PM|LINK
thanks PeteNet! One last follow up, which I forgot to include in my last reply... can I do this *without* needing to use whereParameters? (dynamically build a sting to be used for where property without using that WhereParameters collection and putting @placeHolders in the query string). I don't remember why but I think I encountered some issues with dealing with whereparameters on the fly...
I'll be marking your responses as answers... as usual, they are :)
I also posted another question a few minutes ago related to the overall design of this search page I'm working on... this where property question is one of two ways I will wind up using to handle dynamically building my where conditions... the other is using the PredicateBuilder class. So, with your help, I now know I have two options for that part... now my last problem seems to be a more high level page design issue, which is what I just added another post on.... my linqdatasource selecting event seems to be becoming a monolithic procedure encompassing all my page logic, and I assume that probably is not right :/
PeteNet
All-Star
81342 Points
11398 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 04:21 PM|LINK
yes.
here's an example that is working:
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="LinqDataSourceConcepts.NWindOrdersDataContext" TableName="Orders" onselecting="LinqDataSource2_Selecting"> </asp:LinqDataSource>protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { LinqDataSource2.Where = "CustomerID = " + "\"" + DropDownList1.SelectedValue + "\"" + " && ShipCity = \"London\" "; GridView1.DataSourceID = "LinqDataSource2"; GridView1.DataBind(); }protected void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e) { if (DropDownList1.SelectedIndex != -1) { LinqDataSource2.Where = "CustomerID = " + "\"" + DropDownList1.SelectedValue + "\"" + " && ShipCity = \"London\" "; } }:) inadvertently so might seem, but the other way would have been to do it without the LDS, as you have mentioned, and that too would have a single 'entry' point, so to speak. (although right now your implementation does seem to be well matched to your requirements - maybe that's what you should think about?)
Peter
c0pe
Member
319 Points
435 Posts
Re: linqdatasource.where property troubles
Sep 19, 2010 09:04 PM|LINK
Thanks Peter, always appreciated.