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