Inserting with a SqlDataSource Using uniqueidentifier Parameters

Published 16 November 05 03:24 PM | Scott Mitchell

Scenario: Creating a page with an editable GridView or insertable DetailsView (or FormView) that uses a SqlDataSource whose contens are generated through the Configure Data Source wizard. Specifically, the SqlDataSouce's SELECT statement returns a column of type uniqueidentifier (a GUID), and the wizard is configured to automatically generate the INSERT, UPDATE, and DELETE statements as well.

Problem: When updating or inserting into the GridView or DetailsView (or FormsView) I get an “Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query” exception.

Solution: According to FormView Control, SqlDataSource, and 'Procedure or Argument has too Many Arguments Specified', It appears that this particular problem (or something similar) was a bug back in Beta 2 that (sadly) made it to RTM.

The crux of the problem, it appears, is that the <asp:Parameter> value for the uniqueidentifier field is, by default, set to Type=”Object”. To fix this, simply remove the Type property altogether. That is, change the SqlDataSource parameter setting from something like:

<asp:SqlDataSource ...>
<InsertParameters>
<asp:Parameter Name=”UserId” Type=”Object” />
...
</InsertParameters>
</asp:SqlDataSource>

to:

<asp:SqlDataSource ...>
<InsertParameters>
<asp:Parameter Name=”UserId” />
...
</InsertParameters>
</asp:SqlDataSource>

This change worked for me; once the Type was removed the exception ceased and the updates/inserts worked as expected.

Hope this helps some folks...

Filed under:

Comments

# Thilo said on November 18, 2010 09:14 AM:

This problem still exists with VS2010. But that solution works.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Archives

My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.