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."
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With