 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

February 26th, 2010, 11:16 PM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Error Message Linking DDL to Gridview
I get the error message when I'm trying to filter results using 'where' so that clicking on a dropdownlist results in the results showing up in a gridview (aka pg 401).
Instead of the SqlDataSource in the book I'm using a LinqDataSource.
I get this error message
Quote:
|
Operator '==' incompatible with operand types 'Int32' and 'Object'
|
(I don't get the '=' that shows up in the book)
I'm using my own tables. I've gotten this error message before but am not sure why its happened. Is there a problem with my tables?
Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="GridViewDataSource" DataKeyNames="CatId">
<Columns>
<asp:BoundField DataField="CatId" HeaderText="CatId"
ReadOnly="True" SortExpression="CatId" InsertVisible="False" />
<asp:BoundField DataField="CatName" HeaderText="CatName"
SortExpression="CatName" />
<asp:BoundField DataField="ProductId" HeaderText="ProductId"
SortExpression="ProductId" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="GridViewDataSource" runat="server"
ContextTypeName="TreatmentReviewsDataContext" TableName="Categories"
Where="CatId == @CatId">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CatId"
PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
|
|

February 27th, 2010, 06:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Apparently something is "an object" instead of an integer. What's the data type of CatId in your Linq to SQL model?
Imar
|
|

February 27th, 2010, 09:45 PM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It shows 'int' in the original table and 'int not nullable Identity' in the Linq To Sql model (?)
|
|

February 28th, 2010, 05:22 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Are you sure you got your logic right? Your GridView seems to display products *in* a specific category, yet the data source seems to query that very Categories TableName.
If that doesn't solve it, can you make a complete working example available (download, mail) somewhere?
Cheers,
Imar
|
|

March 4th, 2010, 11:20 PM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm not sure how to download all this. You were right about my logic but I think I've corrected that.
I made another small test. I created two small tables:
- Category (CatId (int)/CatName)
- Reviews (ReviewId (int)/Title/Comments/CatID (int)).
I made a FK by dragging CatId from Category table to CatId to the Reviews table.
The First DDL works fine but when I when I try to apply the filter I get that same error message. I would add that the where filter does not give me the chance to chose "=" , it only gives me the option for "==" whether I have an int or a string (?). The book shows "=".
Here's the error message: Operator '==' incompatible with operand types 'Int32' and 'Object'
Thanks for your help :)
Here's the markup:
Code:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1>Reviews Management</h1>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="LinqDataSource1" DataTextField="CatName" DataValueField="CatId">
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="WroxTestDataContext" Select="new (CatId, CatName)"
TableName="Categories">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="LinqDataSource2">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True"
SortExpression="Title" />
<asp:BoundField DataField="Comments" HeaderText="Comments" ReadOnly="True"
SortExpression="Comments" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="WroxTestDataContext" Select="new (Title, Comments)"
TableName="Reviews" Where="CatId == @CatId">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CatId"
PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
|
|

March 5th, 2010, 05:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
== is the equality operator in C#; = is used in VB. Maybe the book shows the VB version? Can't recall exactly and I don't know what image you're referring to...
And where exactly do you see this "=="? In which dialog?
Imar
|
|

March 13th, 2010, 08:02 PM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I found the problem. I honestly don't understand it but the fix did work. Here's a link to it and the post I found it on is below. I ended up adding default="0" to Where parameters of the second linqdatasource that uses the DropdownList
http://weblogs.asp.net/rajbk/archive...xceptions.aspx
Prerequisite: LinqDataSource & SqlDataSource Master/Details
When working with the LinqDataSource, you may get the exceptions listed below.
1. Operator '==' incompatible with operand types 'Int32' and 'Object'
The exception occurs because anytime a ControlParameter in the WhereParameters collection (IDictionary<string, object>) is null, it gets treated as type Object causing the LINQ dynamic expression parser comparison to fail. Consider the code snippet below:
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="DataClassesDataContext"
Select="new (OrderID, ProductID, UnitPrice, Quantity, Discount, Order)" TableName="Order_Details"
Where="OrderID == @OrderID">
<WhereParameters>
<asp:ControlParameter ControlID="GridView1" Name="OrderID" PropertyName="SelectedValue"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
Quote:
This is part of the classic Master/Details scenario where the LinqDataSource fetches the Order Details based on the OrderID selected in the GridView1.
When the page loads the first time, the OrderID ControlParameter is equal to GridView1.SelectedValue. GridView1.SelectedValue is null since no record has been selected in the GridView1 yet. Unfortunately, the LinqDataSource still attempts to fetch the data. The Linq expression parser treats the null parameter as type Object and the comparison fails because it was expecting type Int32.
What we need here is a way to prevent the Select from occurring when any parameter is null. The LinqDataSource, unlike the SqlDataSource, for some reason, does not have a CancelSelectOnNullParameter property. This property when set to true, will cancel the select when any parameter in the SelectParameters collection is null.
We can implement this by handling the Selecting event of the LinqDataSource and call the Cancel method when any WhereParameter is null like so:
protected void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
{
if (kvp.Value == null)
{
e.Cancel = true;
return;
}
}
}
|
Unfortunately not long after that the program starting not loading the datacontext giving me a "Cannot load Type TreatReviewsDataContext" even though I hadn't touched the data context. Another page gave me the same error. I created a new datacontext to no avail; it simply will not load it. (?)
|
|

March 14th, 2010, 05:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Code:
I found the problem. I honestly don't understand it but the fix did work.
It makes sense if you consider the fact the drop down list or other control you're using has no value when the page loads the first time. In other words, you try to filter on something that has no value. By setting the default value to 0, you now filter on that value (and pobably get no records back).
But I guess the error message is misleading. Cannot convert null to Int32 would have been more appropriate... ;-)
Quote:
|
Unfortunately not long after that the program starting not loading the datacontext giving me a "Cannot load Type TreatReviewsDataContext" even though I hadn't touched the data context.
|
Impossible for me to say something about this other than saying it's impossible for me to say something about it. Clearly something has changed but I can't see what or what you need to do to fix it. Google for the exact error message (but leave out your own type name) and see if that brings up something useful...
Cheers,
Imar
|
|

March 16th, 2010, 07:06 PM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Finally fixed it. I googled linqdatasource and 'cannot load type' and came up with a namespace problem. I went to the Object Browser, found out the namespace (C!), attached it to it and it loaded right up. I don't know why this happened but I'm glad its working now. Original error message is below by the way.
Code:
Server Error in '/PR1' Application.
Could not load type 'TreatReviewsDataContext'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Could not load type 'TreatReviewsDataContext'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): Could not load type 'TreatReviewsDataContext'.]
System.Web.Compilation.BuildManager.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +8829125
System.Web.UI.WebControls.LinqDataSourceView.get_ContextType() +93
[InvalidOperationException: Could not find the type specified in the ContextTypeName property of LinqDataSource 'LinqDataSource1'.]
System.Web.UI.WebControls.LinqDataSourceView.get_ContextType() +209
System.Web.UI.WebControls.LinqDataSourceView.CreateContextAndTable(DataSourceOperation operation) +680
System.Web.UI.WebControls.LinqDataSourceView.CreateContextAndTableForSelect() +91
System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +383
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
System.Web.UI.Adapters.ControlAdapter.CreateChildControls() +15
System.Web.UI.Control.EnsureChildControls() +128
System.Web.UI.Control.PreRenderRecursiveInternal() +44
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
Last edited by Cort; March 16th, 2010 at 07:21 PM..
|
|

March 17th, 2010, 02:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Great. Good to hear it's working now.
Cheers,
Imar
|
|
 |