| |
|
|
|
|
|
|
|
|
|
 |    |  |
 | |  |
 | |  |
 | |  |
 | |  |
 |
|
|
| Re: BC30469: Reference to a non-shared member requires an object reference |
|
|
When you call GetRoleByName, it is returning a RoleInfo object. If that role doesn't exist, the RoleInfo object will be Nothing. So, before you ask for its RoleID, make sure it isn't Nothing.
Dim role as RoleInfo = objRoleController.GetRoleByName(RoleName)
if role IsNot Nothing then
...
else
'create role
end if
DotNetNuke.Security.Roles.RoleController().GetUsersByRoleName() seems to be your best bet for getting the users of RoleB assigned to RoleA. You'll get an ArrayList of UserInfo objects. You can access each item in the ArrayList with
For Each user as UserInfo in usersList
'call AddUserRole() for the current user object
..Next
Hope that helps,

The leading provider of DotNetNuke support, training and custom module development. |
|
|
|
 |  |
|
|
| Re: BC30469: Reference to a non-shared member requires an object reference |
|
|
I am still getting "ERROR: Object reference not set to an instance of an object." If the Role does exist, it works perfect. Only when having to create the role, it gives error.
Protected Sub CreateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateButton.Click
Try
Dim objRoles As New RoleController
Dim UserName1 As String = Coach.Text
Dim CoachRoleName As String = (SchoolNS.Text & "-Coach")
Dim userEmail As String = Membership.GetUser(UserName1).Email
Dim userId As Integer = UserController.GetUserByName(0, UserName1).UserID
Dim roleId As String = objRoles.GetRoleByName(PortalSettings.PortalId, CoachRoleName).RoleID
Dim objRoleInfo As New RoleInfo
Dim objRoleController As DotNetNuke.Security.Roles.RoleController
objRoleController = New DotNetNuke.Security.Roles.RoleController
If roleId IsNot Nothing Then
'do nothing
Else
objRoleInfo.AutoAssignment = False
objRoleInfo.BillingFrequency = "Month"
objRoleInfo.BillingPeriod = 6
objRoleInfo.Description = SchoolNS.Text & "Coach Role"
objRoleInfo.IsPublic = False
objRoleInfo.PortalID = 0
objRoleInfo.RoleID = roleId
objRoleInfo.RoleName = CoachRoleName
objRoleInfo.ServiceFee = "0.00"
objRoleController.AddRole(objRoleInfo)
End If
objRoleController.AddUserRole(PortalSettings.PortalId, userId, roleId, Null.NullDate, Null.NullDate)
errorLabel.Text = "Completed"
Catch ex As Exception
errorLabel.Text = "ERROR: " & ex.Message.ToString()
End Try
End Sub |
|
|
|
 |  |
|
|
| Re: BC30469: Reference to a non-shared member requires an object reference |
|
|
It's happening on the line:
Dim roleId As String = objRoles.GetRoleByName(PortalSettings.PortalId, CoachRoleName).RoleID
You can't call .RoleID is the role is Nothing. It's here that you need to, first, get a reference to the RoleInfo object, second, test if the role IsNot Nothing, and, only then, ask for its RoleID. Does that make sense?

The leading provider of DotNetNuke support, training and custom module development. |
|
|
|
 |  |
|
|
| Re: BC30469: Reference to a non-shared member requires an object referencerian |
|
|
Brian, that does make perfect sense but I cannot figure out how to do what you suggested.
I got the reference (Dim roleId As String = objRoles.GetRoleByName(PortalSettings.PortalId, CoachRoleName).RoleID)
I checked if it IsNot Nothing (If roleId IsNot Nothing Then)
Now what do you mean "and, only then, ask for its RoleID" how can it have a RoleId if it is Null? dosnt it have to assign a new RoleID for the new role? I tried just leaving out the objRoleInfo.RoleId but that caused the same error.
|
|
|
|
 |  |
|
|
| Re: BC30469: Reference to a non-shared member requires an object referencerian |
|
|
You need to assign the RoleInfo object returned from GetRoleByName into its own variable, and test if it is null before you call .RoleId. You do not have to assign a RoleId to the new role. After your save the new role object, it should be assigned a RoleId. I've reworked your code a bit for you. I added checks for null on both the user and role objects, consolidated some duplicate objects that weren't needed, and added the use of Me.PortalId instead of 0 (you never know when your code will get used in a way differently that you originally intended, so it's best to be safe, especially when PortalId is so easy to get).
Protected Sub CreateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateButton.Click
Try
Dim objRoles As New RoleController()
Dim UserName1 As String = Coach.Text
Dim CoachRoleName As String = (SchoolNS.Text & "-Coach")
Dim user as UserInfo = UserController.GetUserByName(Me.PortalId, UserName1)
if user Is Nothing Then
Return
End If
Dim userId As Integer = user.UserID
Dim objRoleInfo as RoleInfo = objRoles.GetRoleByName(PortalSettings.PortalId, CoachRoleName)
if objRoleInfo Is Nothing Then
objRoleInfo = New RoleInfo()
objRoleInfo.AutoAssignment = False
objRoleInfo.BillingFrequency = "Month"
objRoleInfo.BillingPeriod = 6
objRoleInfo.Description = SchoolNS.Text & "Coach Role"
objRoleInfo.IsPublic = False
objRoleInfo.PortalID = Me.PortalId
objRoleInfo.RoleName = CoachRoleName
objRoleInfo.ServiceFee = "0.00"
objRoles.AddRole(objRoleInfo)
End If
objRoleController.AddUserRole(Me.PortalId, userId, objRoleInfo.RoleId, Null.NullDate, Null.NullDate)
errorLabel.Text = "Completed"
Catch ex As Exception
errorLabel.Text = "ERROR: " & ex.Message.ToString()
End Try
End Sub
Hope it helps,

The leading provider of DotNetNuke support, training and custom module development. |
|
|
|
|  |
 | |  |
 | |  |
 | |  |
|  |
| |
 |
|
These Discussion Forums are dedicated to the discussion of the DotNetNuke Web Application Framework.
For the benefit of the community and to protect the integrity of the project, 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 DotNetNuke.
2. Discussion or promotion of DotNetNuke product releases under a different brand name are strictly prohibited.
3. No Flaming or Trolling.
4. No Profanity, Racism, or Prejudice.
5. Site Moderators have the final word on approving/removing a thread or post or comment.
6. English language posting only, please.
|
| |
 |
|
|
|
|
|
|
|