-->
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.  [ 6 posts ] 
Author Message
 Post subject: NHibernate + GridView Problem
PostPosted: Tue Jun 20, 2006 6:53 am 
Newbie

Joined: Thu Jun 15, 2006 5:27 am
Posts: 13
Hibernate version: 1.0.2

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: SQL Server 2005

Hello,
I'm new to NHibernate and I need some help.

In my ASP.NET Web Form, I've got a gridVeiw bound to a database through NHibernate.
I implemented Adding, Inserting, Updating and Deleting methods.

Actually, my problem is that when I delete an Item from my gridView, it is temporarily deleted because when I try to delete another item, the item previosly deleted reappears.

Please really need some help, does the problem come from NHibernate configuration of from my gridview.

Code:
public void Delete(Collabs c, int id)
        {

            ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Delete("from Collabs c where c.Id = :Id", id, NHibernate.NHibernateUtil.Int32);
                tx.Commit();
            }
            catch
            {
                tx.Rollback();
            }
            finally
            {
                session.Close();
            }
        }


Code:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            object id = this.GridView1.DataKeys[e.RowIndex].Value;
            Collabs col = new Collabs();

            if (id != null)
            {
                col.Id = Convert.ToInt32(id);
                agence.Delete(col, col.Id);
                GridView1.DataSource = agence.PrintAll();
                Bindings();
            }
        }


Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 12:41 pm 
Newbie

Joined: Tue Jun 20, 2006 12:39 pm
Posts: 1
I think you have to call session.Flush(); before tx.Commit(); so the changes are persisted in the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 1:44 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
no session.Flush() is called automatically with a tx.Commit(). personally i think you might be dealing with viewstate issues... can you post the code for the Bindings() method?

also, why are you creating a new Collabs object? you aren't really using it. sending it to your Delete() method seems superfluous.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 5:12 am 
Newbie

Joined: Thu Jun 15, 2006 5:27 am
Posts: 13
Thanks for your replies. this is the code for Bindings() method and all my aspx page code behind:
Code:
namespace NHWebComplete
{
    public partial class _Default : System.Web.UI.Page
    {
       
        static string bdd = ConfigurationManager.AppSettings["BaseDeDonnées"];
        static string cnxStr = ConfigurationManager.AppSettings[bdd + ".ChaîneDeConnexion"];

        Agence agence = new Agence(bdd, cnxStr);

        private void Bindings()
        {
            GridView1.DataSource = agence.PrintAll();
            GridView1.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            agence.FillTable();
            if (!Page.IsPostBack)
            {
                Bindings();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            IList list = agence.PrintAll();
            list.Insert(0, new Collabs());
            GridView1.EditIndex = 0;
            GridView1.DataSource = list;
            GridView1.DataBind(); 
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            object id = this.GridView1.DataKeys[e.RowIndex].Value;
            Collabs col = new Collabs();

            if (id != null)
            {
                col.Id = Convert.ToInt32(id);
                agence.Delete(col, col.Id);
                GridView1.DataSource = agence.PrintAll();
                Bindings();
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
        }

        protected  void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            object id = this.GridView1.DataKeys[e.RowIndex].Value;
            Collabs col = new Collabs();

            TextBox lbNom = (TextBox)row.Cells[1].Controls[0];
            TextBox lbService = (TextBox)row.Cells[2].Controls[0];
            TextBox lbDate = (TextBox)row.Cells[3].Controls[0];

            col.Nom = lbNom.Text;
            col.Service = lbService.Text;
            col.Date = DateTime.Parse(lbDate.Text);

            if (id != null)
            {
                col.Id = Convert.ToInt32(id);
                agence.SaveOrUpdate(col, col.Id);
            }

            GridView1.EditIndex = -1;
            Bindings();
        }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bindings();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bindings();
        }
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 9:06 am 
Beginner
Beginner

Joined: Mon Mar 06, 2006 2:19 pm
Posts: 42
Location: Belo Horizonte, Brazil
After looking to your code I have some questions

1) Your Bindinds() method calls PrintAll() and your RowDeleting event calls PrintAll() and Bindings(). Why?

Code:
private void Bindings()
{
    GridView1.DataSource = agence.PrintAll();
    GridView1.DataBind();
}


Code:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
            GridViewRow row = GridView1.Rows[e.RowIndex];
            object id = this.GridView1.DataKeys[e.RowIndex].Value;
            Collabs col = new Collabs();

            if (id != null)
            {
                col.Id = Convert.ToInt32(id);
                agence.Delete(col, col.Id);
                GridView1.DataSource = agence.PrintAll();
                Bindings();
            }
}


2) After each deleted row you fill the whole grid again?

3) How do you know what is the order of fields returned by your PrintAll() method? Let's suppose a table:

Cliente
{
id int (pk),
name varchar(50)
}

How do you know that PrintAll() will always return id at IList[0] and name at IList[1]? isn't your GridView first column bound to IList[0]?


Sorry but any of these questions will help you but I was curious about that.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 10:17 am 
Newbie

Joined: Thu Jun 15, 2006 5:27 am
Posts: 13
I'll try to answer:

1)I forgot to remove one PrinAll()
2)After each row deleted, I want to show the entries of my table
3)I think it does it automatically


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

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.