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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close