tuckerjt07 tuckerjt07 - 3 months ago 10
SQL Question

ASP.NET Role and Member Management

I am using the built in role and membership management tool for asp.net on my website. I am currently in the process of upgrading the abilities of the site so that the site admin can add/delete roles, and manage the access of the different roles from inside a page on the site. However, when I execute

System.Web.Security.Roles.CreateRole(roleName); //Where roleName is a string value of the role that I am creating, and it is not a duplicate name.


I get the error message

System.Data.SqlClient.SqlException: Conversion failed when converting from a character string to uniqueidentifier.


The site is running on shared hosting and I am using the ASP.NET tables, triggers, and stored procedures. What is odd is that I have had no trouble updating users to roles, creating users, deleting users, resetting passwords, etc. using the built in methods. Is it possible that the stored procedure that the system created for me was in some way flawed and is not returning all of the data that it needs to execute? If not, then what else could be the cause of the issue?

Answer

Here's the Create Role SP:

CREATE PROCEDURE [dbo].[aspnet_Roles_CreateRole]
@ApplicationName  nvarchar(256),
@RoleName         nvarchar(256)

AS BEGIN DECLARE @ApplicationId uniqueidentifier SELECT @ApplicationId = NULL

DECLARE @ErrorCode     int
SET @ErrorCode = 0

DECLARE @TranStarted   bit
SET @TranStarted = 0

IF( @@TRANCOUNT = 0 )
BEGIN
    BEGIN TRANSACTION
    SET @TranStarted = 1
END
ELSE
    SET @TranStarted = 0

EXEC dbo.aspnet_Applications_CreateApplication @ApplicationName, @ApplicationId OUTPUT

IF( @@ERROR <> 0 )
BEGIN
    SET @ErrorCode = -1
    GOTO Cleanup
END

IF (EXISTS(SELECT RoleId FROM dbo.aspnet_Roles WHERE LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId))
BEGIN
    SET @ErrorCode = 1
    GOTO Cleanup
END

INSERT INTO dbo.aspnet_Roles
            (ApplicationId, RoleName, LoweredRoleName)
     VALUES (@ApplicationId, @RoleName, LOWER(@RoleName))

IF( @@ERROR <> 0 )
BEGIN
    SET @ErrorCode = -1
    GOTO Cleanup
END

IF( @TranStarted = 1 )
BEGIN
    SET @TranStarted = 0
    COMMIT TRANSACTION
END

RETURN(0)

Cleanup:

IF( @TranStarted = 1 )
BEGIN
    SET @TranStarted = 0
    ROLLBACK TRANSACTION
END

RETURN @ErrorCode

END