Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conversion failed when converting from a character string to uniqueidentifier.

Posted on 2009-05-07
5
Medium Priority
?
1,559 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 1000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 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