1

CreateUserWizard で追加のプロパティを呼び出していますが、GetUser() を呼び出すと、

「このコマンドに関連付けられている DataReader が既に開いているため、最初に閉じる必要があります」と表示されます

        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

Closeupdate ステートメントを発行する前に、リーダーに連絡する必要があります。

    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 に答える