ASP.net Roles

Capodecina
Permabanned
Joined
31 Dec 2003
Posts
5,172
Location
Barrow-In-Furness
I'm setting up my page that will manage roles, and i'm struggling with deleting a role that contain users.

I would like a label to be set when the user tries to delete a role that is not empty, the code i'm using is:

When I also delete a roll that isn't populated, the label sets to "Role is populated", but the role gets deleted.

Code:
 If Roles.DeleteRole(DropDownList1.SelectedItem.ToString(), True) Then
            Label2.Text = "Role is populated"
        Else
            Label2.Text = ""
            Roles.DeleteRole(DropDownList1.SelectedItem.ToString())
            RolesDefinedDropDownList()

This doesn't work and caused the page to crash with the error "Role cannot be deleted because there are users present in it".

Anyone got any ideas?

For reference here is the code for the rest of the page:

Code:
Partial Class Management_RoleManagement
    Inherits System.Web.UI.Page

    Protected Sub RolesDefinedDropDownList()
        DropDownList1.DataSource = Roles.GetAllRoles
        DropDownList1.DataBind()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            RolesDefinedDropDownList()
        End If
    End Sub

   
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Roles.RoleExists(TextBox1.Text) = True Then
            Label1.Text = "Role already exists"
        Else
            Roles.CreateRole(TextBox1.Text)
            TextBox1.Text = ""
            Label1.Text = ""
            RolesDefinedDropDownList()
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Roles.DeleteRole(DropDownList1.SelectedItem.ToString(), True) Then
            Label2.Text = "Role is populated"
        Else
            Label2.Text = ""
            Roles.DeleteRole(DropDownList1.SelectedItem.ToString())
            RolesDefinedDropDownList()
        End If
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        BulletedList1.DataSource = Roles.GetUsersInRole(DropDownList1.SelectedItem.ToString())
        BulletedList1.DataBind()
    End Sub
End Class
 
Last edited:
The Roles.DeleteRole() method is used to delete the specific role, it doesn't check to see if it is populated. Your if statement reads...

if delete this role then
show 'Role is populated'
else
show ''
delete this role
rebind dropdownlist function

What you should be doing is checking to see if there is anything in that role, which you already have done for button 1 using RoleExists.
RoleExists checks to see if the role you specified is stored anywhere in the role database, then returns a true or false value (but you already knew that ;)).
 
I thought if you set the boolean value of DeleteRole is will throw an exception allowing me to use it like I have, but I guess i've misunderstood.

I'll do what I did for Button 1 then, thanks Kevin you fountain of ASP knowledge.
 
Wardie said:
I thought if you set the boolean value of DeleteRole is will throw an exception allowing me to use it like I have, but I guess i've misunderstood.

Ah so it should, my bad. Never really done much with the ASP.NET Membership, apart from using the login controls.

But one thing I have noticed also is that you are using the SelectedItem property for the dropdown list. SelectedItem returns the index/location of that item in the dropdown list, which is an integer value. The DeleteRole requires a string value, so you should change this to SelectedValue instead.
 
But one thing I have noticed also is that you are using the SelectedItem property for the dropdown list. SelectedItem returns the index/location of that item in the dropdown list, which is an integer value. The DeleteRole requires a string value, so you should change this to SelectedValue instead.

Nope - you're getting confused there. SelectedIndex returns an integer value representing the position in the list, whereas SelectedItem returns an instance of a ListItem object, containing the display name of the list item and it's value.


Wardie said:
What's the difference between SelectedItem and SelectedIndex then?

See above.
 
I can't work out how to do what i've said in the original post using any of the other roles functions.

Have you got any idea elk?

Code:
 If Roles.DeleteRole(DropDownList1.SelectedValue) = True Then
            Label2.Text = "Role is populated"
        Else
            Label2.Text = ""
            Roles.DeleteRole(DropDownList1.SelectedValue)
            RolesDefinedDropDownList()

Not sure why that doesn't work, the boolean in Roles.DeleteRole should throw an exception if it's populated rather than just crashing.
 
I can't work out how to do what i've said in the original post using any of the other roles functions.

Have you got any idea elk?

Code:
 If Roles.DeleteRole(DropDownList1.SelectedValue) = True Then
            Label2.Text = "Role is populated"
        Else
            Label2.Text = ""
            Roles.DeleteRole(DropDownList1.SelectedValue)
            RolesDefinedDropDownList()

Not sure why that doesn't work, the boolean in Roles.DeleteRole should throw an exception if it's populated rather than just crashing.

Yes it should, and it sounds like it is. What do you mean when you say 'crashing'? It's just from the code you've posted I can't see where you're actually handling the exception which is being thrown.
 
I'm not a .NET programmer, but isn't this following line completely redundant:

Code:
If Roles.DeleteRole(DropDownList1.SelectedValue) = True Then

If the statement Roles.DeleteRole(DropDownList1.SelectedValue) evaluate to true then that is sufficient for the if statement, you don't need to say if (statement evaluating to true)=true or if (statement evaluating to false)=true.
 
Yes it should, and it sounds like it is. What do you mean when you say 'crashing'? It's just from the code you've posted I can't see where you're actually handling the exception which is being thrown.

I posted the error I was getting in the first post, I have a feeling i've not fully understood how it works. Where do I have to handle the exeception?

I thought if the answer is true (because there is users in the selected roll) it would perform the next line, if it's not true it would perform the else section and delete the roll.

I'm not a .NET programmer, but isn't this following line completely redundant:

Code:
If Roles.DeleteRole(DropDownList1.SelectedValue) = True Then

If the statement Roles.DeleteRole(DropDownList1.SelectedValue) evaluate to true then that is sufficient for the if statement, you don't need to say if (statement evaluating to true)=true or if (statement evaluating to false)=true.

You can either use Roles.DeleteRole = True or False, if you opt for true it will throw an exception if users are present within the role.
 
Ahh you have to catch the exceptions. Looking at a sample of code that shows how to do this:

(Didn't know there was a Try command)

Code:
 Sub btnDeleteRole_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If (lbxAvailableRoles.SelectedIndex <> -1) Then
      Try
        Roles.DeleteRole(lbxAvailableRoles.SelectedValue)

        lblResults.Text = Nothing
        lblResults.Visible = False
      Catch ex As Exception
        lblResults.Text = "Could not delete the role: " + Server.HtmlEncode(ex.Message)
        lblResults.Visible = True
      End Try
    End If

    RefreshAvailableRolesListBox()
  End Sub

Full sample code: http://quickstarts.asp.net/QuickSta.../aspnet/samples/security/Add_Delete_Roles.src
 
elkdanger said:
Nope - you're getting confused there. SelectedIndex returns an integer value representing the position in the list, whereas SelectedItem returns an instance of a ListItem object, containing the display name of the list item and it's value.

I'm not trusting Google again, I did a search for DropDownList SelectedItem one of the results came back with SelectedIndex, which I happened to click on :o

Wardie said:
Not sure why that doesn't work, the boolean in Roles.DeleteRole should throw an exception if it's populated rather than just crashing.

You haven't put the boolean value as a parameter in the DeleteRole Method.
It should be Roles.DeleteRole(String, Boolean), if you exclude the boolean, it's going to think you just want to delete that role.
Maybe you need to check the returned value as well.
 
I'm not trusting Google again, I did a search for DropDownList SelectedItem one of the results came back with SelectedIndex, which I happened to click on :o



You haven't put the boolean value as a parameter in the DeleteRole Method.
It should be Roles.DeleteRole(String, Boolean), if you exclude the boolean, it's going to think you just want to delete that role.
Maybe you need to check the returned value as well.

Read my last post, all working now :D
 
You haven't put the boolean value as a parameter in the DeleteRole Method. It should be Roles.DeleteRole(String, Boolean).

That's why I said your statement was redundant, if it's a parameter it makes sense but saying true=true is redundant.
 
That's why I said your statement was redundant, if it's a parameter it makes sense but saying true=true is redundant.

Yeah I understand now, that's an error on my part without a doubt.

Like I said though, found an easier way of doing it now :)

Thanks folks.
 
Back
Top Bottom