Hi All,
Im trying to creating user and assign them to a role in dnn using c#, what i have keeps throwing object reference exception and un-enable to create the user. The Role that i need to assign the user is coming from a query string. when ever i execute the code, it doesnt insert into the user table and its gives the error below. Please i need your help if there is something i'm missing.
Thanks
--------------------ERROR--------------
Error: Manage is currently unavailable. DotNetNuke.Services.Exceptions.ModuleLoadException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object. at DotNetNuke.Security.Roles.RoleController.AddUserRole(Int32 portalId, Int32 userId, Int32 roleId, RoleStatus status, Boolean isOwner, DateTime effectiveDate, DateTime expiryDate) at DotNetNuke.Security.Roles.RoleController.AddUserRole(Int32 portalId, Int32 userId, Int32 roleId, DateTime effectiveDate, DateTime expiryDate) at Christoc.Modules.ProjectName.ManageCTSO.ManageTSOMsg.Page_Load(Object sender, EventArgs e) in c:\dnndev.me\DesktopModules\ProjectName\ManageCTSO\ManageTSOMsg.ascx.cs:line 87 --- End of inner exception stack trace ---
----------------------------------------
string space = " ";
UserInfo oUser = new UserInfo();
oUser.PortalID = this.PortalId;
oUser.IsSuperUser = false;
oUser.FirstName = Session["fname"].ToString();
oUser.LastName = Session["lname"].ToString();
oUser.Email = Session["email"].ToString();
oUser.Username = Session["username"].ToString();
oUser.DisplayName = Session["fname"].ToString() + space.ToString() + Session["lname"].ToString();
//Fill MINIMUM Profile Items (KEY PIECE)
oUser.Profile.PreferredLocale = PortalSettings.DefaultLanguage;
// oUser.Profile.PreferredTimeZone =DateTime.Parse(PortalSettings.TimeZoneOffset);
oUser.Profile.FirstName = oUser.FirstName;
oUser.Profile.LastName = oUser.LastName;
//Set Membership:
UserMembership oNewMembership = new UserMembership();
oNewMembership.Approved = true;
oNewMembership.CreatedDate = System.DateTime.Now;
oNewMembership.Email = oUser.Email;
oNewMembership.IsOnLine = false;
oNewMembership.Username = oUser.Username;
oNewMembership.Password = UserController.GeneratePassword(8);
oUser.Membership = oNewMembership;
if (UserCreateStatus.Success == UserController.CreateUser(ref oUser))
{
RoleController oDnnRoleController = new RoleController();
//Get the role information
RoleInfo oCurrentRole = oDnnRoleController.GetRoleByName(this.PortalId, Request.QueryString["TSORole"].ToString());
// RoleInfo oCurrentRole1 = oDnnRoleController.GetRoleByName(this.PortalId, " Subscribers");
//Assign to user
oDnnRoleController.AddUserRole(this.PortalId, oUser.UserID, oCurrentRole.RoleID, Null.NullDate, Null.NullDate);
oDnnRoleController.DeleteUserRole(this.PortalId, int.Parse(oUser.UserID.ToString()), oCurrentRole.RoleID);
}