I am developing with VS.NET 2005, and I am using data layer table adapters to interact with my data. So the following shows how I added code to John's code, so to add my users to roles:
'Add towards the end of 'cmdImportUsers_Click'
If createStatus = UserCreateStatus.Success Then
responseBuilder.Append(String.Format("User '{0}' created successfully", oUserInfo.Username))
responseBuilder.AppendLine()
'and add to my custom role
Dim da As New UserProfileTableAdapter
Dim daUsers As New UsersTableAdapter
Dim i As Integer
i = daUsers.GetUserID(GetXmlItemValue(xmlItem, "Username"))
'You can retrieve role ids with SP GetRoles
'In this case the roleID for my custom role is '3'
da.AddUserToRole(0, i, 3) '(portalId, userId, RoleId)
'responseBuilder.Append(String.Format("User '{0}' added to Custom Role "))
'responseBuilder.AppendLine()
Else
responseBuilder.Append(String.Format("<br />User '{0}' creation failed: {1}", oUserInfo.Username, [Enum].GetName(GetType(UserCreateStatus), createStatus)))
End If
All 'GetUserID' does is :
SELECT UserID
FROM Users
WHERE Username = @Param1
And 'AddUserToRole(0, i, 3)' does is call a new SP I created called AddUserRoleNoDate (based on SP AddUserRole)
AddUserRoleNoDate sql is like so:
USE [YOURDATABASE]
GO
/****** Object: StoredProcedure [dbo].[AddUserRoleNoDate] Script Date: 06/29/2007 09:00:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddUserRoleNoDate]
@PortalId int,
@UserId int,
@RoleId int
AS
DECLARE @UserRoleId int
SELECT @UserRoleId = null
SELECT @UserRoleId = UserRoleId
FROM dbo.UserRoles
WHERE UserId = @UserId AND RoleId = @RoleId
IF @UserRoleId IS NOT NULL
BEGIN
SELECT @UserRoleId
END
ELSE
BEGIN
INSERT INTO dbo.UserRoles (
UserId,
RoleId
)
VALUES (
@UserId,
@RoleId
)
SELECT SCOPE_IDENTITY()
END