Membership.Updateuser没有真正更新数据库

问题描述:

我目前正在为我的web应用程序开发一个基于表单身份验证的会员系统。Membership.Updateuser没有真正更新数据库

我用集成工具创建了一些用户,登录完全正常。但是现在我想要做的是给管理员创建,修改,删除用户的能力。

所以这是我有现在:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim muc As MembershipUserCollection = Membership.GetAllUsers() 

    ComboBox1.DataSource = muc 
    ComboBox1.DataValueField = "UserName" 
    ComboBox1.DataTextField = "UserName" 
    ComboBox1.DataBind() 
End Sub 

Protected Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged 

    Dim userName As String = ComboBox1.SelectedValue 

    Dim mu As MembershipUser = Membership.GetUser(userName) 

    Dim userRoles As String() = Roles.GetRolesForUser(userName) 

    tbComments.Text = mu.Comment 
    tbEmail.Text = mu.Email 
    lblUserName.Text = mu.UserName 
End Sub 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
    Dim userName As String = ComboBox1.SelectedValue 
    Dim mu As MembershipUser = Membership.GetUser(userName) 

    If Not mu Is Nothing Then 
     Try 
      mu.Comment = tbComments.Text 
      Membership.UpdateUser(mu) 

      mu.Email = tbEmail.Text 
      Membership.UpdateUser(mu) 

      mu.IsApproved = True 
      Membership.UpdateUser(mu) 

      mu = Nothing 
     Catch ex As Exception 
      Console.WriteLine(ex.ToString()) 
     End Try 
    End If 

    DetailPanel.Visible = False 
End Sub 

的问题是,记录似乎并不在数据库中进行更新。 我读了this blog entry后多次致电Membership.UpdateUser,但没有任何改变。

我在调试时发现了一件奇怪的事情,那就是当我输入Button1_Click方法时,Membership.GetUser(userName)会返回我先前的尝试值!我真的不明白我错过了什么。

有人有线索吗?

在此先感谢!

+0

好没关系......我只是来包装组合框初始化一个'如果没有Page.IsPostBack然后...如果结束'声明。组合框的第一个元素始终更新,因为它的选定值是该元素。 我该怎么办?删除问题? – Shimrod 2010-04-27 14:15:13

+0

而且,不要删除问题,甚至是您自己的问题,特别是当您自己解决问题时。 SO是为每个人提供Q/A,而不仅仅是提问者。 – 2010-04-27 14:49:31

+0

你是对的,我完全同意这一点;-)我只是问,因为在这种情况下,这个问题并没有真正与我的问题挂钩:-) 无论如何,你的答案是非常有帮助的,我会接受它!谢谢 :-) – Shimrod 2010-04-28 08:33:04

第一:

你举的博客条目只是错误

这里是您所呼叫的方法,告诉我,如果你看到它需要被调用多次更新多个属性指示:

public override void UpdateUser(MembershipUser user) 
{ 
    if (user == null) 
    { 
     throw new ArgumentNullException("user"); 
    } 
    SecUtility.CheckParameter(ref user.UserName, true, true, true, 0x100, "UserName"); 
    string email = user.Email; 
    SecUtility.CheckParameter(ref email, this.RequiresUniqueEmail, this.RequiresUniqueEmail, false, 0x100, "Email"); 
    user.Email = email; 
    try 
    { 
     SqlConnectionHolder connection = null; 
     try 
     { 
      connection = SqlConnectionHelper.GetConnection(this._sqlConnectionString, true); 
      this.CheckSchemaVersion(connection.Connection); 
      SqlCommand command = new SqlCommand("dbo.aspnet_Membership_UpdateUser", connection.Connection); 
      command.CommandTimeout = this.CommandTimeout; 
      command.CommandType = CommandType.StoredProcedure; 
      command.Parameters.Add(this.CreateInputParam("@ApplicationName", SqlDbType.NVarChar, this.ApplicationName)); 
      command.Parameters.Add(this.CreateInputParam("@UserName", SqlDbType.NVarChar, user.UserName)); 
      command.Parameters.Add(this.CreateInputParam("@Email", SqlDbType.NVarChar, user.Email)); 
      command.Parameters.Add(this.CreateInputParam("@Comment", SqlDbType.NText, user.Comment)); 
      command.Parameters.Add(this.CreateInputParam("@IsApproved", SqlDbType.Bit, user.IsApproved ? 1 : 0)); 
      command.Parameters.Add(this.CreateInputParam("@LastLoginDate", SqlDbType.DateTime, user.LastLoginDate.ToUniversalTime())); 
      command.Parameters.Add(this.CreateInputParam("@LastActivityDate", SqlDbType.DateTime, user.LastActivityDate.ToUniversalTime())); 
      command.Parameters.Add(this.CreateInputParam("@UniqueEmail", SqlDbType.Int, this.RequiresUniqueEmail ? 1 : 0)); 
      command.Parameters.Add(this.CreateInputParam("@CurrentTimeUtc", SqlDbType.DateTime, DateTime.UtcNow)); 
      SqlParameter parameter = new SqlParameter("@ReturnValue", SqlDbType.Int); 
      parameter.Direction = ParameterDirection.ReturnValue; 
      command.Parameters.Add(parameter); 
      command.ExecuteNonQuery(); 
      int status = (parameter.Value != null) ? ((int) parameter.Value) : -1; 
      if (status != 0) 
      { 
       throw new ProviderException(this.GetExceptionText(status)); 
      } 
     } 
     finally 
     { 
      if (connection != null) 
      { 
       connection.Close(); 
       connection = null; 
      } 
     } 
    } 
    catch 
    { 
     throw; 
    } 
} 

二:

你重新绑定列表每回发。这只需要完成一次,并且列表存储在视图状态中。

这里是一个工作实现:

UpdateUser.aspx

<%@ Page Language="vb" %> 

<script runat="server"> 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

     ' be sure that DropDownList1.AutoPostBack = true 

     If Not IsPostBack Then 
      BindUserList() 
     End If 
    End Sub 


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
     DisplayDetails(ComboBox1.SelectedValue) 
    End Sub 


    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
     Dim approved As Boolean = True 
     Dim username As String = ComboBox1.SelectedValue 
     Dim comments As String = tbComments.Text 
     Dim email As String = tbEmail.Text 

     UpdateUser(username, approved, comments, email) 
    End Sub 

    Private Sub BindUserList() 
     '' you only need to databind once, the datasource is stored in viewstate 
     Dim muc As MembershipUserCollection = Membership.GetAllUsers() 

     ComboBox1.DataSource = muc 
     ComboBox1.DataValueField = "UserName" 
     ComboBox1.DataTextField = "UserName" 
     ComboBox1.DataBind() 
     '' initialize the selection 
     ComboBox1_SelectedIndexChanged(ComboBox1, EventArgs.Empty) 
    End Sub 


    Private Sub DisplayDetails(ByVal userName As String) 
     Dim mu As MembershipUser = Membership.GetUser(userName) 

     Dim userRoles As String() = Roles.GetRolesForUser(userName) 

     tbComments.Text = mu.Comment 
     tbEmail.Text = mu.Email 
     lblUserName.Text = mu.UserName 
    End Sub 

    Private Sub UpdateUser(ByVal userName As String, ByVal approved As Boolean, ByVal comments As String, ByVal email As String) 
     Dim mu As MembershipUser = Membership.GetUser(userName) 
     If Not mu Is Nothing Then 
      Try 

       mu.Comment = comments 
       mu.Email = email 
       mu.IsApproved = approved 

       Membership.UpdateUser(mu) 

       ErrLabel.Text = "" 
      Catch ex As Exception 
       ErrLabel.Text = ex.Message 
      End Try 
     End If 
    End Sub 
</script> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:DropDownList ID="ComboBox1" runat="server" AutoPostBack="True"> 
     </asp:DropDownList> 
    </div> 
    <p> 
     UserName:<asp:Label ID="lblUserName" runat="server" Text=""></asp:Label><br /> 
     Email:<asp:TextBox ID="tbEmail" runat="server"></asp:TextBox><br /> 
     Comments:<asp:TextBox ID="tbComments" runat="server"></asp:TextBox><br /> 
    </p> 
    <asp:Button ID="Button1" runat="server" Text="Update" Height="26px" Width="61px" /><br /> 
    <asp:Label ID="ErrLabel" runat="server" Text=""></asp:Label> 
    </form> 
</body> 
</html>