Connection closing after repeater databinding

瘦欲@ 提交于 2020-02-02 15:59:26

问题


I recently changed the way the connection worked on my web app and now I'm facing something that I don't understand.

I hava a page that call a function called "ItemGet" in the Page_Load and it work perfectly when there is no data in the first repeater (Repeater1). When I click on a button that reload the page with different data (I know there is data in the repeater), the connection is closed automatically right after that same repeater (Repeater 1). The problem, is that there is another repeater right after (RepeaterTopTen) that need the same connection. I closed manually the connection right after the call to that function but at least I need the connection to stay open during all the function.

Do any of you know the reason why it closed itself and what I can do to prevent it to close at this time?

Here is the code :

private void ItemsGet(string csCategory, string csTimeFrame)
{
    DataSet data;

    if (csCategory == null)
    {
        data = m_database.GetPost(Tools.GetPostLang(), Session["TimeFrame"].ToString());
        Page.Title = m_database.GetTranslation(509);
    }
    else
    {
        data = m_database.GetPost(Convert.ToInt32(csCategory), Tools.GetPostLang(), Session["TimeFrame"].ToString());
        Page.Title = m_database.GetTranslation(508) + m_database.GetCategoryName(Convert.ToInt32(csCategory));
    }         

    // Populate the repeater control with the Items DataSet
    PagedDataSource objPds = new PagedDataSource();
    objPds.DataSource = (DataView)(data.Tables[0].DefaultView);

    // Indicate that the data should be paged
    objPds.AllowPaging = true;

    // Set the number of items you wish to display per page
    objPds.PageSize = 5;

    // Set the PagedDataSource's current page
    if (CurrentPage != 0)
        objPds.CurrentPageIndex = CurrentPage;
    else
        objPds.CurrentPageIndex = 0;

    lblCurrentPage.Text = m_database.GetTranslation(423) + (CurrentPage + 1).ToString() + m_database.GetTranslation(422) + objPds.PageCount.ToString();

    // Disable Prev or Next buttons if necessary
    btnPrev.Enabled = !objPds.IsFirstPage;
    btnNext.Enabled = !objPds.IsLastPage;

    Repeater1.DataSource = objPds;
    Repeater1.DataBind();

    DataSet dataTopTen = m_database.GetTopTenUser();
    RepeaterTopTen.DataSource = dataTopTen;
    RepeaterTopTen.DataBind();

}

回答1:


Because no one answer that one, I created a function that check if the connection is open and if not, it open it. I checked carefully that it was closed each time.




回答2:


If you declare your database object and call the open method on the database connection in your Page_Load method, then the database connection will remain open during your entire page life cycle. With your database connection declared in this function, the database object goes out of scope and gets closed when your function ends.



来源:https://stackoverflow.com/questions/1565564/connection-closing-after-repeater-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!