These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Rebind asp.net gridview after update
PostPosted: Fri Apr 25, 2008 7:17 am 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
Hi,
I have a regular gridview that I bind codebehind, like:
Code:
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            BindGrid();
        }

}

private void BindGrid()
{
        IList<PageMenu> menus = Bll.GetExpandedPageMenus();
        gvMenus.DataSource = menus;
        gvMenus.DataBind();
}


And after I make an update in the gridviews rowcomand I need to rebind the grid.
Code:
protected void gvMenus_RowCommand(object sender, GridViewCommandEventArgs e)
{     
        Bll.Sort(e.CommandName, Convert.ToInt32(e.CommandArgument));
        BindGrid();
}

Here is the problem.
The grid rebinds but with wrong values.
It binds with the values before the update!
I need to refresh the page to get the correct values.
I guess the problem must be something with the session handling.

When in the pages lifecycle does the update happens?
At Application_EndRequest?


My update method:
Code:
public dbReturnValues Update(T objectToSave)
        {
            dbReturnValues retVal = new dbReturnValues();
            ISession session = NHibernateHelper.GetCurrentSession();

            ITransaction tx = null;
            try
            {
                tx = session.BeginTransaction();
                session.Update(objectToSave);
                //NHibernateHelper.GetCurrentSession().Flush();           
                tx.Commit();
                retVal.Success = true;
            }
            catch (Exception)
            {
                if (tx != null)
                    tx.Rollback();
                throw;
            }
            finally
            {
                if (tx != null)
                    tx.Dispose();
            }

            return retVal;
        }


My session and sessionfactory code:
Code:
namespace SD.Dao
{
    public sealed class NHibernateHelper
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static readonly ISessionFactory sessionFactory;

        static NHibernateHelper()
        {
            //log4net.Config.XmlConfigurator.Configure();
            NHibernate.Cfg.Environment.UseReflectionOptimizer = false;
            sessionFactory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;
            }

            return currentSession;
        }

        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                // No current session
                return;
            }

            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);

        }

        public static void CloseSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}


And I close the session in a httpmodule:
Code:
namespace SD.Dao
{
    public class HttpRequestModule : IHttpModule
    {
       
        public String ModuleName
        {
            get { return "NHibernate.Utility.HttpRequestModule"; }
        }

        public void Init(HttpApplication application)
        {
            application.EndRequest += (new EventHandler(this.Application_EndRequest));
        }

        private void Application_EndRequest(Object source, EventArgs e)
        {
            NHibernateHelper.CloseSession();
        }

        public void Dispose()
        { }
    }
}


How can I change this code so it will make the update and then bind the grid with new values?

Thanks,
Doggelito


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.