Scott on Writing

Musings on technical writing...

Inserting with a SqlDataSource Using uniqueidentifier Parameters

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...

posted on Wednesday, November 16, 2005 3:24 PM

Feedback

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 11/18/2005 6:46 AM Soulhuntre

Man, this totally saved me YEARS of time. Thanks!

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 11/18/2005 7:10 AM Soulhuntre

Hmm... no, there are still issues. It's pretty frustrating. Unless the GUID column is visible in the DataGrid it seems to fail utterly.

Grrrr.

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 11/18/2005 7:35 AM Soulhuntre

My semi solution (different than this one)

http://www.soulhuntre.com/items/date/2005/11/18/net-issues-using-uniqieidentifier-as-a-primary-key-in-vs2005/

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 12/2/2005 5:40 AM Gregor Suttie

Still doesnt work - any other ideas?

http://forums.asp.net/1129192/ShowPost.aspx

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 12/5/2005 6:54 AM Paul Mc

It seems that the fix didn't work for me. I'm using a stored proc with optional parameters defaulted with NULL values. I found that if you set the CancelSelectOnNullParameters to False on the SqlDataSource properties, it will return a result set.

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 12/11/2005 5:39 PM Tyrus

What finally fixed it for me was (as wierd as it seems) Make sure your stored procedures arguments are named after the columns being dealt with exactly. I wasted a whole day on this. Query analyzer had it working just fine but the formview (gridview detailsview any view) would always return that error.

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 12/12/2005 6:21 AM Felix

Thank you for this very nice described help, it worked for me...

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 1/18/2006 1:35 AM Vittorio

For me it worked with the attribut <asp:Parameter Name=”UserId” Type=”Object” />


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

Vittorio

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 2/9/2006 5:17 AM Ed

If the control encapsulating the uniqueidentifier is visible, this doesn't seem to work. However, adding the uniqueidentifier as a datakey to wither the gridview or formview works like a charm.

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 3/1/2006 1:22 PM Clumsy Hamster

I've seen this work both ways and no way in the same application. Guess it is still a buggy thing.

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 3/31/2006 1:00 AM Coward

Using VS2005 version 8.0.50727.42 (RTM.050727-4200) and .Net version 2.0.50727 this works fine. Just make sure you remove it from the Insert:Parameters not the Delete:Parameters because that makes no difference what so ever, surprisingly! Doh!

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 7/7/2006 11:24 PM Dale S

Man, another hour of life wasted...ah well...very glad you posted this. Solved my problem. Thx

# VS 2005: Gotcha DataGrid Dataset and uniqueidentifier (GUIDS) 8/3/2006 10:32 AM Simon Phillips

I've never been a great fan of the point and click nature of the DataGrid in .Net, it all seams too easy...

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 10/26/2006 10:55 PM D-Ball

Paul Mc, your suggestion worked great! Thanks!

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 5/7/2007 7:44 PM Bean

Removing the Type alone did not help. I generate the GUIDs when I insert rows. So, go to the SQLDataSource and edit the InsertQuery. Replace the @<GuidparameterName> parameter in the query with 'newid()'. The newid() command will tell Sql Server to generate the new guid for the inserted row.

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 10/24/2007 3:11 PM lata

thanks after a lot of research on the web, this solution worked perfectly for me, thanks a bunch

# re: Inserting with a SqlDataSource Using uniqueidentifier Parameters 12/6/2007 1:09 AM Noopur

Thanks... it really works n it has solved my prob within a sec...

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles