我正在使用asp.net会员资格并尝试更新用户详细信息但出现以下错误:
Error 2 Overload resolution failed because no accessible 'New' accepts this number of arguments.
以下是完整的.vb代码:
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
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
在我之前:
Dim user As MembershipUser
并收到错误:
对象引用未设置为 对象的实例。描述:一个 未处理的异常发生在 当前网络的执行 请求。请查看堆栈跟踪 有关错误的更多信息 它起源于代码。
异常详细信息: System.NullReferenceException:Object 引用未设置为的实例 对象
来源错误:
第20行:'无参数 构造函数第21行:第22行:
user.Email = DirectCast(e.NewValues(0),String) 第23行:user.Comment = DirectCast(e.NewValues(1),String) 第24行:user.IsApproved = CBool函数(e.NewValues(2))
我为缺乏编程知识而道歉。您能否指出我需要更改我的代码以更新用户详细信息?
由于
答案 0 :(得分:0)
在Membership.GetUser(username)
中,您需要添加第二个参数IsUserOnline(Boolean):
Membership.GetUser(username, true)
变量Membership
初始化为Membership.Provider
?