0

我有一个显示 2 个用户控件的朋友列表页面。
第一个控件显示所有当前好友,而另一个显示当前好友请求。

用户按以下顺序控制负载:

- 当前好友- 好友
请求

他们每个人都以几乎相同的方式搜索数据库,使用相同的结构List<>,并绑定到非常相似Repeaters的 .

我得到的当前问题是:

ExecuteReader requires an open and available Connection. The connection's current state is closed.

然而,我在每个读者面前打开连接,并在使用后关闭它。由于相似之处;我将发布其中一个UserControl's 代码。将根据要求发布更多信息。

protected void Page_Load(object sender, EventArgs e)
    {
        GetFriend();
    }

    /// <summary>
    /// Get the current friends
    /// </summary>
    private void GetFriend()
    {
        try
        {
            List<FriendRequests> friendRequest = new List<FriendRequests>();

            // Get friend added by another
            const string strSql = "SELECT [id], [origionalId], [dateFriendAdded] FROM vwMemberFriendStatus WHERE contactId=@member AND accepted=1";

            SqlCommand sqlComm = new SqlCommand(strSql, DataConn.Connect()) { CommandType = CommandType.Text };

            sqlComm.Parameters.Add(new SqlParameter("@member", SqlDbType.Int)).Value =
                SiteSession.Current.NicknameId;

            SqlDataReader rdr = sqlComm.ExecuteReader();


            while (rdr.Read())
            {
                friendRequest.Add(new FriendRequests
                {
                    IsOrigional = false,
                    FriendId = rdr["id"].ToString(),
                    FriendName = Member.GetMemberName(rdr["origionalId"].ToString()),
                    DateAdded = DateTime.Parse(rdr["dateFriendAdded"].ToString()).ToString("dd MMMM yy")
                });
            }

            rptFriend.DataSource = friendRequest;
            rptFriend.DataBind();

            rdr.Close();
            DataConn.Disconnect();
            Response.Write("Current - Other <br/>");
        }
        catch (Exception f)
        {
            Response.Write("Current / Other: " + f.Message + "<br/>");
        }

        try
        {
            List<FriendRequests> friendRequest = new List<FriendRequests>();

            // Get friend added by self
            const string strSql =
                        "SELECT [id], [contactId], [dateFriendAdded] FROM vwMemberFriendStatus WHERE origionalId=@member AND accepted=1";

            SqlCommand sqlComm = new SqlCommand(strSql, DataConn.Connect()) { CommandType = CommandType.Text };

            sqlComm.Parameters.Add(new SqlParameter("@member", SqlDbType.Int)).Value =
                SiteSession.Current.NicknameId;

            SqlDataReader rdr = sqlComm.ExecuteReader();

            while (rdr.Read())
            {
                friendRequest.Add(new FriendRequests
                {
                    IsOrigional = true,
                    FriendId = rdr["id"].ToString(),
                    FriendName = Member.GetMemberName(rdr["contactId"].ToString()),
                    DateAdded = DateTime.Parse(rdr["dateFriendAdded"].ToString()).ToString("dd MMMM yy")
                });
            }

            rptFriend.DataSource = friendRequest;
            rptFriend.DataBind();

            rdr.Close();
            DataConn.Disconnect();
            Response.Write("Current - Self <br/>");
        }
        catch (Exception f)
        {
            Response.Write("Current / Self: " + f.Message + "<br/>");
        }
    }
4

0 回答 0