我有一个朋友列表页面,显示2个用户控件 第一个控件显示所有当前的朋友,而另一个显示当前的朋友请求。
用户按此顺序控制加载:
- 当前的朋友
- 友情请求
他们每个人都以几乎相同的方式搜索数据库,使用相同的List<>
结构,并绑定到非常相似的Repeaters
。
我得到的当前问题是:
ExecuteReader requires an open and available Connection. The connection's current state is closed.
然而,我在每个读者面前打开连接,并在使用后关闭它。由于相似之处;我将发布一个UserControl
的代码。会根据要求发布更多内容。
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/>");
}
}