Sunday, February 12, 2012

converting problems

I have a stored procedure which I user to retur an Email-address from aspnet_Membership (DB-table).

Here is the code:

ALTER PROCEDURE dbo.StoredProcedure2
@.user nvarchar(256)
AS
DECLARE @.id uniqueidentifier
SET @.id = ''
SELECT @.id = UserId from aspnet_Users WHERE UserName = @.user
SELECT Email From aspnet_Membership WHERE UserId = @.id

I use StoredProcedure2 in this way:

Dim cmd As SqlCommand = New SqlCommand("StoredProcedure2", MyConnection)
cmd.CommandType = CommandType.StoredProcedure

'send in UserName as @.User to StoredProcedure2
cmd.Parameters.Add("@.user", SqlDbType.NVarChar).Value = User.Identity.Name.ToString()
'Return EmailAddresse
returnValue = cmd.ExecuteScalar()

Message I get is:

"Conversion failed when converting from a character string to uniqueidentifier"

Please help...


where do you have returnValue dimensioned?

it seems to me the problem is that ExecuteScalar is returning a string and returnValue is set as a different data type.

hth,
mcm

|||Remove this line from your stored procedure:

SET @.id = ''

The error indicates you can't convert an empty string to a uniqueidentifier, as empt string doesn't match uniqueidentifier?format.?A column or local variable of uniqueidentifier data type can be initialized to a value in two ways:

a)?Using the NEWID function.
b) Converting from a string constant in the following form (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which each x is a hexadecimal digit in the range 0-9 or a-f). For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value.

If you're not sure what's wrong when executing a SQL statement/SP from application, you can run it with proper value in Query Analyzer/Management Studio for t-shooting.

No comments:

Post a Comment