Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting Error: Inserting data into Custom MembershipUser

I'm attempting to fill in data to my NCCMembershipUser with the following code:

string name = User.Identity.Name;

NCCMembershipUser currentUser = (NCCMembershipUser)NCCMembershipProvider.GetUser(name, true);

currentUser.Salutation = GenderSelect.SelectedValue;
currentUser.FirstName = TextBoxFirstName.Text;
currentUser.LastName = TextBoxLastName.Text;
currentUser.Position = TextBoxPosition.Text;
...

try
{
    NCCMembershipProvider u = (NCCMembershipProvider)Membership.Provider;
    u.UpdateUser(currentUser);
}

I am getting an error "An object reference is required for the non-static field, method, or property 'System.Web.Security.MembershipProvider.GetUser(string, bool)'"

If I instead use Membership.GetUser() (without the name string) to access the current user, it gives me a casting error, and GetUser() appears it cannot be overridden.

Edit:

The casting error I get is "[A]NCC.App_Code.NCCMembershipProvider cannot be cast to [B]NCC.App_Code.NCCMembershipProvider."

like image 764
RyanJMcGowan Avatar asked Oct 08 '22 13:10

RyanJMcGowan


1 Answers

The error tells you that the GetUser method isn't static, so it cannot be invoked without an instance of the NCCMembershipProvider class.

You have to grab your provider earlier in your method:

string name = User.Identity.Name;
NCCMembershipProvider u = (NCCMembershipProvider)Membership.Provider;

NCCMembershipUser currentUser = (NCCMembershipUser)u.GetUser(name, true);

currentUser.Salutation = GenderSelect.SelectedValue;
currentUser.FirstName = TextBoxFirstName.Text;
currentUser.LastName = TextBoxLastName.Text;
currentUser.Position = TextBoxPosition.Text;
// ...

try
{
    u.UpdateUser(currentUser);
}
like image 72
Adam Lear Avatar answered Oct 13 '22 10:10

Adam Lear