getting error when tring to update user details, asp.net membership
I am using asp.net membership and trying to update a users details but get the following error:
Error 2 Overload resolution failed because no accessible 'New' accepts this number of arguments.
Below is the full .vb code:
Partial Class FamilyAdmin_edit_user
Inherits System.Web.UI.Page
Private username As String
Dim user As MembershipUser
Private Sub Page_Load()
username = Request.QueryString("username")
If username Is Nothing OrElse username = "" Then
Response.Redirect("users.aspx")
End If
User = Membership.GetUser(username)
UserUpdateMessage.Text = ""
End Sub
Protected Sub UserInfo_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs)
'Need to handle the update manually because MembershipUser does not have a
'parameterless constructor
user.Email = DirectCast(e.NewValues(0), String)
user.Comment = DirectCast(e.NewValues(1), String)
user.IsApproved = CBool(e.NewValues(2))
Try
' Update user info:
Membership.UpdateUser(user)
' Update user roles:
UpdateUserRoles()
UserUpdateMessage.Text = "Update Successful."
e.Cancel = True
UserInfo.ChangeMode(DetailsViewMode.[ReadOnly])
Catch ex As Exception
UserUpdateMessage.Text = "Update Failed: " + ex.Message
e.Cancel = True
UserInfo.ChangeMode(DetailsViewMode.[ReadOnly])
End Try
End Sub
Private Sub Page_PreRender()
' Load the User Roles into checkboxes.
UserRoles.DataSource = Roles.GetAllRoles()
UserRoles.DataBind()
' Disable checkboxes if appropriate:
If UserInfo.CurrentMode <> DetailsViewMode.Edit Then
For Each checkbox As ListItem In UserRoles.Items
checkbox.Enabled = False
Next
End If
' Bind these checkboxes to the User's own set of roles.
Dim userRoles__1 As String() = Roles.GetRolesForUser(username)
For Each role As String In userRoles__1
Dim checkbox As ListItem = UserRoles.Items.FindByValue(role)
checkbox.Selected = True
Next
End Sub
Private Sub UpdateUserRoles()
For Each rolebox As ListItem In UserRoles.Items
开发者_如何学JAVA If rolebox.Selected Then
If Not Roles.IsUserInRole(username, rolebox.Text) Then
Roles.AddUserToRole(username, rolebox.Text)
End If
Else
If Roles.IsUserInRole(username, rolebox.Text) Then
Roles.RemoveUserFromRole(username, rolebox.Text)
End If
End If
Next
End Sub
Public Sub DeleteUser(ByVal sender As Object, ByVal e As EventArgs)
'Membership.DeleteUser(username, false); // DC: My apps will NEVER delete the related data.
Membership.DeleteUser(username, True)
' DC: except during testing, of course!
Response.Redirect("manage_members.aspx")
End Sub
Public Sub UnlockUser(ByVal sender As Object, ByVal e As EventArgs)
' Dan Clem, added 5/30/2007 post-live upgrade.
' Unlock the user.
User.UnlockUser()
' DataBind the GridView to reflect same.
UserInfo.DataBind()
End Sub
End Class
Before i had:
Dim user As MembershipUser
and got the error:
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 20: 'parameterless constructor Line 21: Line 22:
user.Email = DirectCast(e.NewValues(0), String) Line 23: user.Comment = DirectCast(e.NewValues(1), String) Line 24: user.IsApproved = CBool(e.NewValues(2))
I apologise for my lack of programming knowledge. Could you please point out where i need to change my code to be able to update the users details?
Thanks
In Membership.GetUser(username)
you need to add a second parameter IsUserOnline (Boolean):
Membership.GetUser(username, true)
The variable Membership
is initialized with a Membership.Provider
?
精彩评论