Skip to content


ASP .NET, LINQ, GridViews, and GUID Errors

I found a simple problem today. Here is the deal, there were several times where I had to base a LinqDataSource off a Guid that I would set in the Page_Load(). I created an<asp:HiddenField /> to hold the value so all my DataSources could pull it from the control. The HiddenField control’s value is stored as a string, so I would have to convert the guid to a string. After doing this, I was getting the following error:

Operator ‘==’ incompatible with operand types ‘Guid’ and ‘String’

After looking around in my code and some forums, I found the answer: Linq is strongly cast and Visual Studio 2008 most like set the “where” clause to something like this:

<asp:LinqDataSource ID=”ldsProducs” runat=”server”

        ContextTypeName=”CartDataContext” EnableDelete=”True” EnableInsert=”True”

        EnableUpdate=”True” OrderBy=”Product_UPC1″ TableName=”Product_UPCs”

        Where=”Product_IID == @Product_IID”>

        <WhereParameters>

            <asp:ControlParameter ControlID=”hfProductIID” Name=”Product_IID”

                PropertyName=”Value” Type=”Object” />

        </WhereParameters>

    </asp:LinqDataSource>

 However, because Linq is strongly cast you can’t perform an == operand on a String and Guid, so I changed the following:

<asp:LinqDataSource ID=”ldsProducs” runat=”server”

        ContextTypeName=”CartDataContext” EnableDelete=”True” EnableInsert=”True”

        EnableUpdate=”True” OrderBy=”Product_UPC1″ TableName=”Product_UPCs”

        Where=”Product_IID == Guid(@Product_IID)”>

        <WhereParameters>

            <asp:ControlParameter ControlID=”hfProductIID” Name=”Product_IID”

                PropertyName=”Value” Type=”Object” />

        </WhereParameters>

    </asp:LinqDataSource>

 Now it’ll convert the HiddenField’s value to a Guid and compair.

Related Posts

  1. ASP .NET, GridViews, SqlDataSource, and Guid Parameters Another thing I ran into today. I actually ran into it awhile back but since I ran into it again today I’d thought I’d post on it to help anyone who happens to find it. Problem You have a GridView and a SqlDataSource that takes 1 or more Guids as...
  2. LINQ and NOT IN Queries Found an excellent blog post on how to do NOT IN scenarios using .Contains(). Bookmark ThisSubscribeBloglinesBlogmarksDiggdel.icio.usFacebookFurlNewsVineRedditStumbleUponTechnorati...
  3. ASP .NET 2.0, GridViews, HyperLinkField, and JavaScript I've found some interesting notes on ASP .NET's GridView, it's limitations and work arounds. For work I was looking for a way to execute JavaScript from clicking a link on a GridView Cell. An example would be to having a list of transactions and wanting to click a link to...
  4. MySQL - Does Table Exist w/o Throwing Errors There are times where you would like to know if a table exists before executing an query. Most solutions require having MySQL throw an error saying "table does not exist," but I prefer a cleaner way. I found on this forum post a clean way to do it: PLAIN TEXT...

Posted in Programming. Tagged with , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.

Powered by WP Hashcash