Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Getting StartedGetting StartedCreating Role always throws error System.NullReferenceExceptionCreating Role always throws error System.NullReferenceException
Previous
 
Next
New Post
9/23/2016 10:59 AM
 
Hello DNN friends,

I'm just trying to create a new Role in C#. But the code you can see below always raises the following error:
An exception of type 'System.NullReferenceException' occurred in DotNetNuke.dll but was not handled in user code

After searching in this forum and a lot of other places I still don't know why the following code raises an error when I try to create a new role.

Hopefully someone can point me in the right direction.

Thanks a lot for your help,
Sven


var portalId = 0;
var roleName = "Test Role 1";
var roleInfo = RoleController.Instance.GetRoleByName(portalId, roleName);

if (roleInfo == null)
{
roleInfo = new RoleInfo();
roleInfo.PortalID = portalId;
roleInfo.RoleID = -1;
roleInfo.RoleGroupID = -1;
roleInfo.RoleName = roleName;
roleInfo.IsPublic = false;
roleInfo.AutoAssignment = false;
roleInfo.Status = RoleStatus.Approved;
roleInfo.SecurityMode = SecurityMode.SecurityRole;
roleInfo.IsSystemRole = false;
RoleController.Instance.AddRole(roleInfo);
}
 
New Post
9/24/2016 3:56 PM
 
Hi folks,

after having a look at the DNN sources the solution was really simple, but in some way also annoying.

role.RoleID =
Convert.ToInt32(dataProvider.AddRole(role.PortalID,
role.RoleGroupID,
role.RoleName.Trim(),
role.Description.Trim(),
role.ServiceFee,
role.BillingPeriod.ToString(CultureInfo.InvariantCulture),
role.BillingFrequency,
role.TrialFee,
role.TrialPeriod,
role.TrialFrequency,
role.IsPublic,
role.AutoAssignment,
role.RSVPCode,
role.IconFile,
UserController.Instance.GetCurrentUserInfo().UserID, (int)role.Status,
(int)role.SecurityMode,
role.IsSystemRole));

The problematic line is: role.Description.Trim()

In my code above I didn't define a description for roleInfo. The Trim() function runs into an error of course if Description is null. There are two things which surprise me nevertheless.

1. The error message is not really helpful.
2. The CreateRole function should also check the parameters Description or RoleName if they are null using Requires.NotNull(). That way the user of the DNN API would immediately see what's going wrong.


Hopefully this will help anybody else if a similar problem arises.

All the best,
Sven
 
New Post
9/25/2016 6:16 AM
 
the error occurs in your code (where you are calling trim prior to passing it to AddRole), which fails, even if CreateRole accepts Null value for this parameter, i.e, you need to make sure to pass at least an empty string or trim only, if not null.

Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
New Post
9/25/2016 11:32 AM
 
Hi Sebastian,

thanks for your response, I really appreciate that.

But just to be clear. The second post related to code that is part of the DNN Library and not my code. To be more precise it's the function CreateRole() in the class DotNetNuke.Security.Roles.DNNRoleProvider.

Since this is part of the public DNN API I just wanted to emphasize that the preconditions could be better checked using the guard methods DotNetNuke.Common.Requires.NotNull for the role.Description parameter. That way the client of this API would immediately recognize what went wrong. I think there are a lot more places in the DNN sources that could be more "defensive".

But anyway - now everything works as it should and maybe the code snippet in the first post helps someone else trying to create Roles in C#. The snippet is absolutely fine.

BTW thanks for all your answers you already made during the past decade! :) You are really very helpful in these forums!
 
New Post
9/26/2016 9:35 AM
 
Oops, sorry, I misread your post.
please create an item in the open DNN issue tracker at support.dnnsoftware.com and report your findings.

Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Getting StartedGetting StartedCreating Role always throws error System.NullReferenceExceptionCreating Role always throws error System.NullReferenceException


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out