I am using ASP.NET and Entity Framework Code First approach! I changed the Role table of EF to add a Priority column and i also added a Many-Many relationship between Role table and another table called Controls ! I have page that want to assing each User Control (one or more ) Roles! so that they can only be visible by that roles. Here's the code that i used to add Many-Many relationship ! the VS gives me this error:

An exception of type 'System.NullReferenceException' occurred in TestFinal.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

i am absoulty sure that both of the Objects are not Null ! here is the code:

 ApplicationDbContext context = new ApplicationDbContext();
        context.Controls.Find(Convert.ToInt32(ControlDropDown.Text)).Roles.Add(UserAndRole.findRole(RoleDropDown.Text));//(ApplicationRole)context.Roles.Find(RoleDropDown.Text));
        var role = UserAndRole.findRole(RoleDropDown.Text);
      //  context.myRole.Find(RoleDropDown.Text).controls.Add(context.Controls.Find(Convert.ToInt32(ControlDropDown.Text)));
       // context.Roles.Find
       // role.controls.Add(context.Controls.Find(Convert.ToInt32(ControlDropDown.Text)));
        Contorl control = context.Controls.Find(Convert.ToInt32(ControlDropDown.Text));
        context.Roles.Find(RoleDropDown.Text).controls.Add(control);
        context.SaveChanges();

The Error appears in 9th Line !

NOTE

Since it is the first item that i am adding a Many-Many relationship both controls and Roles collections are NUll !

Related posts

Recent Viewed