0

I am creating a basic role manager system where users of my website can be added to specific roles so that they can access exclusive parts of the website.

I have managed to add roles to my database but when I try to assign users to a particular role I get the 'Object reference not set to an instance of an object.' error and I can't work out why.

Here is my code:

protected void btnAssignRole_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Roles.IsUserInRole(lstRoles.SelectedItem.Value))
            {
                Roles.AddUserToRole(lstUsers.SelectedItem.Value, lstRoles.SelectedItem.Value);
                lstRoles.DataBind();
                lstUsers.DataBind();
                litRoleResult.Text = "User added to Role";
            }
            else
            {
                litRoleResult.Text = "User already in this Role";
            }
        }
        catch (Exception ex)
        {
            litRoleResult.Text = ex.Message;
        }
    }

HTML:

<h1>Admin Page Test</h1>
<a href="ProductsAdmin.aspx">Products Admin</a>

<div id="UserAdmin">
    <asp:TextBox ID="txtRoleName" runat="server"></asp:TextBox>
    <asp:Button ID="btnAddRole" runat="server" Text="Create Role" OnClick="btnAddRole_Click" />
    <div>
        <asp:Label ID="Label1" runat="server" Text="Available Roles"></asp:Label>
        <asp:ListBox ID="lstRoles" runat="server" DataSourceID="Roles_DS" DataTextField="RoleName" DataValueField="RoleName"></asp:ListBox>
        <asp:SqlDataSource ID="Roles_DS" runat="server" ConnectionString="<%$ ConnectionStrings:db_1318766_zaraConnectionString %>" SelectCommand="SELECT [RoleName] FROM [vw_aspnet_Roles]"></asp:SqlDataSource>
        <asp:Literal ID="litRole" runat="server"></asp:Literal>
    </div>
    <div>
        <asp:Label ID="Label2" runat="server" Text="Available Users"></asp:Label>
        <asp:ListBox ID="lstUsers" runat="server" DataSourceID="Users_DS" DataTextField="UserName" DataValueField="UserName">
        </asp:ListBox>
        <asp:SqlDataSource ID="Users_DS" runat="server" ConnectionString="<%$ ConnectionStrings:db_1318766_zaraConnectionString %>" SelectCommand="SELECT [UserName] FROM [vw_aspnet_Users]"></asp:SqlDataSource>
    </div>
    <div>
        <asp:Button ID="btnAssignRole" runat="server" Text="Assign Role to User" OnClick="btnAssignRole_Click" />
    </div>
    <div>
        <asp:Button ID="Button3" runat="server" Text="Remove User from Role" />
    </div>
    <div>
        <asp:Button ID="Button4" runat="server" Text="Delete Role(s)" />
    </div>
    <asp:Literal ID="litRoleResult" runat="server"></asp:Literal>
</div>

Help appreciated.

markthornton90
  • 241
  • 1
  • 3
  • 13

2 Answers2

0

It looks like you're using a few objects that must be declared on a Global scale: Roles, lstRoles, lstUsers and litRoleResult all must be initialized at some point because they're not initialized in your method.
My suspicion is that if you set a breakpoint on this method, one of those objects will be null. Also, if lstUsers or lstRoles doesn't have an item selected, I'm pretty sure that calling SelectedItem.Value will throw an exception; ensure that SelectedItem has a value (does it have a .HasValue property?) before you call the value.

EDIT: Sorry, I missed the ASP.NET portion; I'm assuming that your lstUsers and lstRoles are objects on your page. In that case, I'm definitely leaning towards the SelectedItem property being null. Does the list allow multi-select?

  • Hi. Roles is the 'Roles' class within System.Web.Security. I should have added that I have declared this namespace at the top of the .cs file. lstRoles and lstUsers are listbox controls which are bound to my database and litRoleResult is a literal control. I'll amend my original post with the html as well. – markthornton90 Mar 11 '15 at 17:07
  • Just found this on another question; seems there's some issue with DataBind() that causes the `.SelectedValue` to come back as null. See if this helps: http://stackoverflow.com/questions/5859885/selected-item-in-list-box-is-null –  Mar 11 '15 at 17:14
0

RESOLVED

Using the link in Kalmino's comment I amended the following:

In the Page_Load method I changed

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            lstRoles.DataBind();
            lstUsers.DataBind();

        }
    }

to

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lstRoles.DataBind();
            lstUsers.DataBind();

        }
    }

From my original question I also changed the

if (!Roles.IsUserInRole(lstRoles.SelectedItem.Value))

line to

if (!Roles.IsUserInRole(lstUsers.SelectedItem.Text, lstRoles.SelectedItem.Text)) 
markthornton90
  • 241
  • 1
  • 3
  • 13