Solved

Conversion failed when converting from a character string to uniqueidentifier.

Posted on 2009-05-07
5
1,555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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 …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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