Membership.Updateuser 并未真正更新数据库

发布于 2024-08-30 23:45:49 字数 1934 浏览 1 评论 0原文

我目前正在为我的 Web 应用程序开发一个会员系统,该系统基于框架的表单身份验证。

我使用集成工具创建了一些用户,并且登录工作正常。但现在我想做的是赋予管理员创建、修改、删除用户的能力。

所以这就是我现在得到的:

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

问题是数据库中的记录似乎没有更新。 在阅读 此博客条目,但它没有改变任何内容。

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

有人有线索吗?

提前致谢 !

I'm currently working on a membership system for my web application, which is based on forms authentication from the framework.

I created some users with the integrated tool, and the login is perfectly working. But now what I want to do is to give administrator the capability to create, modify, delete users.

So here is what I've got right now:

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

The problem is that the record doesn't seem to be updated in the database.
I made the multiple calls to Membership.UpdateUser after reading this blog entry, but it didn't change anything.

A strange thing I noticed while debugging, is that when I enter the Button1_Click method, Membership.GetUser(userName) returns me values from my precedent attempt ! I don't really understand what I'm missing.

Does someone have a clue ?

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2024-09-06 23:45:49

第一:

您引用的博客条目错误

这是您正在调用的方法,如果您看到需要多次调用它才能更新多个属性的指示,请告诉我:

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>

First:

The blog entry you cite is just wrong.

Here is the method you are calling, tell me if you see an indication that it needs to be called multiple times to update multiple properties:

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;
    }
}

Second:

You are rebinding your list every postback. This needs to be done only once and the list is stored in viewstate.

Here is a working implementation:

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文