Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 26th, 2010, 10:16 PM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>
Reply With Quote
  #2 (permalink)  
Old February 27th, 2010, 05:26 AM
Imar's Avatar
Wrox Author
Points: 67,286, Level: 100
Points: 67,286, Level: 100 Points: 67,286, Level: 100 Points: 67,286, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,228
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

Apparently something is "an object" instead of an integer. What's the data type of CatId in your Linq to SQL model?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old February 27th, 2010, 08:45 PM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It shows 'int' in the original table and 'int not nullable Identity' in the Linq To Sql model (?)
Reply With Quote
  #4 (permalink)  
Old February 28th, 2010, 04:22 AM
Imar's Avatar
Wrox Author
Points: 67,286, Level: 100
Points: 67,286, Level: 100 Points: 67,286, Level: 100 Points: 67,286, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,228
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old March 4th, 2010, 10:20 PM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>
Reply With Quote
  #6 (permalink)  
Old March 5th, 2010, 04:49 AM
Imar's Avatar
Wrox Author
Points: 67,286, Level: 100
Points: 67,286, Level: 100 Points: 67,286, Level: 100 Points: 67,286, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,228
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

== 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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old March 13th, 2010, 07:02 PM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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. (?)
Reply With Quote
  #8 (permalink)  
Old March 14th, 2010, 05:24 AM
Imar's Avatar
Wrox Author
Points: 67,286, Level: 100
Points: 67,286, Level: 100 Points: 67,286, Level: 100 Points: 67,286, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,228
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #9 (permalink)  
Old March 16th, 2010, 07:06 PM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #10 (permalink)  
Old March 17th, 2010, 02:55 AM
Imar's Avatar
Wrox Author
Points: 67,286, Level: 100
Points: 67,286, Level: 100 Points: 67,286, Level: 100 Points: 67,286, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,228
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

Great. Good to hear it's working now.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XmlDataSource DDL and Gridview rthomson77@hotmail.com XML 4 January 21st, 2009 09:57 AM
how can selectedvalue of DDL make other DDL visibl hertendreef ASP.NET 2.0 Professional 4 May 20th, 2008 04:38 PM
Linking GridView to DetailView BOS ASP.NET 1.0 and 1.1 Basics 0 July 7th, 2007 12:40 AM
DDL error cmw231 ASP.NET 2.0 Basics 2 July 17th, 2006 08:07 AM
Linking To Excel - Error timmaher Access 2 August 22nd, 2005 07:36 AM



All times are GMT -4. The time now is 06:14 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.