Update CMS_User fields value and Custom fields value

Amit Srivastava asked on December 20, 2016 06:26

Hi,

I am using custom registration web part with some custom fields and i need to display that fields values in a page. i have finished these things but now i need to update field value on button click i am using UserInfo user = UserInfoProvider.GetUserInfo(MembershipContext.AuthenticatedUser.UserName); FirstNameValue.Text = user.SetValue(CurrentUser.FirstName, value); UserInfoProvider.SetUserInfo(user); API for updating user details but it's displaying some error. Please let me know How to update User details using API.

Correct Answer

Anton Grekhovodov answered on December 20, 2016 07:42

Hi Amit,

I see several errors in this code:

1) user.SetValue(CurrentUser.FirstName, "") the first parameter in this method is a column (property) name of UserInfo class, it can't be CurrentUser.FirstName because, I think, it has dynamic value

2) user.SetValue method with 2 parameters returns a boolean value which is a result of operation, you can't assign it to string property of textbox field FirstNameValue.Text and you can't compile this code.

3) CurrentUser = MembershipContext.AuthenticatedUser, so there isn't any difference between them

The code should be like:

UserInfo user = UserInfoProvider.GetUserInfo(MembershipContext.AuthenticatedUser.UserName);
if (user == null) return; //check if user exists, if not, maybe you need to create him

user.FirstName = value; // or user.SetValue("FirstName", value)
UserInfoProvider.SetUserInfo(user); // or user.Update() if you know that user exists

FirstNameValue.Text = value;

Please, read Kentico documentation and look at Kentico Api references and read about C# programming, because it may help you to save your time)

2 votesVote for this answer Unmark Correct answer

Recent Answers


Amit Srivastava answered on December 20, 2016 07:50

Thank you Anton.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.