1

在我的 CreateUserWizard 上,我正在调用其他属性,当我调用 GetUser() 时,我收到:

“已经有一个打开的 DataReader 与此命令关联,必须先关闭”

Registration.aspx 的代码隐藏:

        NCCMembershipUser currentUser = (NCCMembershipUser)Membership.GetUser();
        Guid id = (Guid)currentUser.ProviderUserKey;

        ...assigning control values to membership values...

        try
        {
            NCCMembershipProvider u = (NCCMembershipProvider)Membership.Provider;
            u.UpdateUser(currentUser);
        }

我不应该使用 GetUser 来访问属性吗?

这是 GetUser 方法:

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT UserID," +
            " Email," +
            " Comment," +
            " PasswordQuestion," +
            " IsApproved," +
            " LastActivityDate," +
            " LastLoginDate," +
            " LastPasswordChangedDate," +
            " CreationDate," +
            " IsLockedOut," +
            " LastLockedOutDate," +
            " UserSalutation," +
            " UserFirstName," +
            " UserLastName," +
            " UserPosition," +
            " UserCompany," +
            " UserCompanyType," +
            " UserCompanyTypeOther," +
            " UserAccountType," +
            " UserAddress1," +
            " UserAddress2," +
            " UserCity," +
            " UserStateProv," +
            " UserPostal," +
            " UserCountry," +
            " UserWebsite," +
            " UserPhone," +
            " UserPhoneExt," +
            " UserFax," +
            " UserIP," +
            " UserIPLastLogin," +
            " IsSubscribed," +
            " LikeAreaRugs," +
            " LikeCarpeting," +
            " LikeCoverings," +
            " LikeComponents," +
            " LikeHotel," +
            " LikeAccessories" +
            " FROM Users WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);

        cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 128).Value = username;
        cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 255).Value = m_ApplicationName;

        NCCMembershipUser u = null;
        SqlDataReader reader = null;

        try
        {
            conn.Open();

            reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                reader.Read();
                u = GetUserFromReader(reader);

                if (userIsOnline)
                {
                    SqlCommand updateCmd = new SqlCommand("UPDATE Users " +
                        "SET LastActivityDate = @LastActivityDate " +
                        "WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);

                    updateCmd.Parameters.Add("@LastActivityDate", SqlDbType.DateTime).Value = DateTime.Now;
                    updateCmd.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value = username;
                    updateCmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar, 255).Value = m_ApplicationName;

                    updateCmd.ExecuteNonQuery();//<<------Error:"There is already an open DataReader associated with this Command which must be closed first."
                }
            }

        }
        catch (SqlException e)
        {
            if (WriteExceptionsToEventLog)
            {
                WriteToEventLog(e, "GetUser(String, Boolean)");

                throw new ProviderException(exceptionMessage);
            }
            else
            {
                throw e;
            }
        }
        finally
        {
            if (reader != null) { reader.Close(); }

            conn.Close();
        }

        return u;
    }
4

1 回答 1

2

在发布更新声明之前,您需要向Close读者说明。

    try
    {
        conn.Open();

        reader = cmd.ExecuteReader();

        if (reader.HasRows)
        {
            reader.Read();
            u = GetUserFromReader(reader);

            reader.Close();

            if (userIsOnline)
            {
                SqlCommand updateCmd = new SqlCommand("UPDATE Users " +
                    "SET LastActivityDate = @LastActivityDate " +
                    "WHERE Email = @Email AND ApplicationName = @ApplicationName", conn);

                updateCmd.Parameters.Add("@LastActivityDate", SqlDbType.DateTime).Value = DateTime.Now;
                updateCmd.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value = username;
                updateCmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar, 255).Value = m_ApplicationName;

                updateCmd.ExecuteNonQuery();
            }
        }
        else
        {
            reader.Close()
        }

    }
于 2012-06-14T04:02:53.587 回答