Solved

Conversion failed when converting from a character string to uniqueidentifier.

Posted on 2009-05-07
5
1,549 Views
Last Modified: 2012-05-06
I am trying to pass a parameter from ASP.NET to a SQL Server Stored procedure.

string strUserID = currentUser.ProviderUserKey.ToString();
Returns "ba275a68-9e77-4301-9d97-7cc7dfecda0d"

SqlCommand cmd = new SqlCommand("usp_addWatchList", conStockSelect);

cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value = strUserID;
cmd.ExecuteNonQuery();
Conversion failed when converting from a character string to uniqueidentifier.

---------
Stored procedure executes successfully from SQL Server Manager
ALTER PROCEDURE [dbo].[usp_addWatchList]
@SymbolID int,
@UserID varchar(50)    

/*
exec usp_addWatchList  
@SymbolID =329,
@UserID = 'BA275A68-9E77-4301-9D97-7CC7DFECDA0D'              
*/

What am I missing?

Thanks,
0
Comment
Question by:Dovberman
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24331093
when adding the parameter it should be passed as SqlDbType.UniqueIdentifier
 Like this:
cmd.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = strUserID;
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24331109
sorry - don't think I read it properly - the other problem you could be having is it not putting the value into the parameter right. I always add parameters like this:
cmd.Parameters.AddWithValue("@UserID", strUserID);
I've had problems in the past the other way where the value was not actually stored in the parameter object properly.
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 250 total points
ID: 24331124
one final thing - it could be in the stored proc when you compare the table field to @UserID - if the table field is a UniqueIdentifier that will fail. Change the parameter in the stored proc to a UniqueIdentifier and add the parameter as System.UniqueIdentifier
Theres a few things to look at anyway :)
0
 

Author Comment

by:Dovberman
ID: 24331502
I set everything to uniqueidentifier.
Error: Failed to convert parameter value from a String to a Guid.

Also tried cmd.Parameters.AddWithValue("@UserID", strUserID);

Error:Conversion failed when converting from a character string to uniqueidentifier.
0
 

Author Comment

by:Dovberman
ID: 24331671
I set everything to uniqueidentifier.

AND
Also tried cmd.Parameters.AddWithValue("@UserID", strUserID);
AND changed the sp from this:
INSERT INTO WatchList
(UserID,SymbolID,PickDatePrice,PickDate,MarketID)
VALUES('@UserID',@SymbolID,23.45,'2009-04-23',3)  

To this:
INSERT INTO WatchList
(UserID,SymbolID,PickDatePrice,PickDate,MarketID)
VALUES(@UserID,@SymbolID,23.45,'2009-04-23',3)  

Now it works.
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calling stored proc in EDMX 11 23
Allow space in this pattern 2 48
Name space syntax error 12 43
Syntax error 9 45
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now