Tuesday, 9 September 2014

DNN: Change DotNetNuke Username from SQL Server

Sometime users request to change their usernames, the reason could be they don't want to lose their activities in the system. There can be many other reasons but the fact is I have deal with these request time to time.So here is a solutions.

If you are going to change the username from SQL Server, make it repeatable, you might want to wrap the syntax in a transaction. You don't want want the two tables to be out of sync. 

Here is sample series of T-Sql statements. Should be easy to convert to a stored procedure

declare @oldName nvarchar(128)
declare @newName nvarchar(128)
declare @error_var int, @rowcount_var int
declare @newNameCount int

select @oldName = 'someUsername'
select @newName = 'newUsername'


begin transaction

select @newNameCount = count(*)
  from Users
  where Username = @newName
if @newNameCount > 0
begin
  RAISERROR('Username already exists. @newName=%s', 10, 1, @newName)
  ROLLBACK TRANSACTION
  RETURN
end

update Users
set Username = @newName
where Username = @oldName

SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
IF @rowcount_var <> 1 OR @error_var <> 0
BEGIN
  RAISERROR('Could not Update User.Username. @oldName=%s', 10, 1, @oldName)
  ROLLBACK TRANSACTION
  RETURN
END


update aspnet_Users
set
  Username = @newName,
  LoweredUserName = LOWER(@newName)
where LoweredUserName = LOWER(@oldName)

SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
IF @rowcount_var <> 1 OR @error_var <> 0
BEGIN
  RAISERROR('Could not Update aspnet_Users.Username. @oldName=%s', 10, 1, @oldName)
  ROLLBACK TRANSACTION
  RETURN
END

Commit transaction
go 


/Adnan


This post is from my old blog posted on May 2012, that unfortunately no longer exist.

0 comments :

Post a comment