0

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?

elixenide
  • 44,308
  • 16
  • 74
  • 100
tuckerjt07
  • 902
  • 1
  • 12
  • 31

2 Answers2

1

Your code isn't correct.

You first create the user by passing in the parameters for that user (username, password, email etc).

You then add that user to a role using the Role Provider

John Mc
  • 2,862
  • 1
  • 22
  • 37
  • I just realized that I put the wrong method name into the code example above. In my code it is .CreateRole(). Thanks, for pointing that out. – tuckerjt07 Oct 30 '12 at 16:07
  • Ok that makes sense. What's the name of the role you're trying to use? – John Mc Oct 30 '12 at 16:08
  • Testing, Emailer, AdvancedMember are three of the names that I have tried, all of them have failed with the same message. – tuckerjt07 Oct 30 '12 at 16:12
  • They should be fine. Is your connectionstring called LocalSqlServer? This thread seems to indicate that it has to be: http://stackoverflow.com/questions/1746068/godaddy-asp-net-membership-database-woes?rq=1 – John Mc Oct 30 '12 at 16:38
  • I'll try that as soon as I get a chance. Thanks, I am about sick of dealing with GoDaddy and all of the little tricks that have to be done to get around their issues. – tuckerjt07 Oct 30 '12 at 16:42
  • It is definitely the stored procedure. I tried manually add the role using the ASP.Net configuration manager and got the unable to convert string to uniqueindentifier message. Do you know where I could get that stored procedure. I do not have a database accessible to me at the moment to run .NET's built in utility against to get it. – tuckerjt07 Oct 30 '12 at 16:53
1

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

John Mc
  • 2,862
  • 1
  • 22
  • 37
  • Bingo, thanks so much for your help! If anyone else has this problem using GoDaddy I highly recommend replacing GoDaddy's CreateRole procedure with this one – tuckerjt07 Oct 30 '12 at 23:11