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.  [ 9 posts ] 
Author Message
 Post subject: Populating Gridview ASP.NET applications
PostPosted: Mon Aug 28, 2006 1:01 am 
Newbie

Joined: Mon Aug 28, 2006 12:47 am
Posts: 6
Location: British Columbia, Canada
I'm new to NHibernate and feel like I was blind, deaf and dumb for now having used it earlier:) It's really a fantastic framework.

I'm trying it out in a web project using Microsoft ASP.NET 2.0. I have a table structure like so:

TransactionTable
(
transid,
employeeid,
statusid,
total
..
..
etc
)

EmployeeTable
(
employeeid
Employeename
...
...
etc
)


I managed to do many-to-one mapping on the Transaction class (which maps to TransactionTable), and I can access the employee properties right from this transaction class:

Transaction trx = (Transaction)session.Load(typeof(Transaction),123);
string empName = trx.Employee.EmployeeName;
double TransTotal = trx.Total;

The problem I have, in in a grid view, if I retrive the transactions like so:

IList allTransactions = session.CreateCriteria(typeof(Transaction)).List();

and bind to gridview:

gridView.Datasource= allTransactions;
gridView.DataBind();

I see all the non-object columns, but if I want to see the EmployeeName associated with the transaction, it is not present. I realize it is a object. Any ideas or suggestions?

Before NHibernate, I would just use a view and get the columns, how I want. How, do I achieve the same effect?
Really appreciate any suggestions--on approaches you have used.

Thanks.


Hanz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 8:19 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
Code:
<asp:TemplateField HeaderText="Unit">
<itemtemplate>
<%# (((AMSLib.ComponentReference)Container.DataItem).Component.Unit.Name)
%>
</itemtemplate>
</asp:TemplateField>



this is an excerpt from one of my GridViews. It is bound to a IList of ComponentReference objects. The ComponentReference object has a property Component (which is a domain object), which has a property Unit which is another domain object. The column displays the value of the Name property (a string). Another approach is to put the code in the event handler for the ItemDataBound event.

hth,
radu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 11:24 am 
Newbie

Joined: Mon Aug 28, 2006 12:47 am
Posts: 6
Location: British Columbia, Canada
Thanks, Radu. This is worked like a charm.


Top
 Profile  
 
 Post subject: RE: Sorting on Column of sub-object
PostPosted: Mon Aug 28, 2006 10:07 pm 
Newbie

Joined: Mon Aug 28, 2006 12:47 am
Posts: 6
Location: British Columbia, Canada
Extending the same example, I can now display the column for the EmployeeName using the method from Radu. However, I can't sort on this column.

As an alternative, I've tried creating a get-only property in the Transaction class for the EmployeeName as follows:

public class Transaction
{
private Employee _employee;

// This is exposed in the mapping file and based on Radu's method.
public Employee Employee
{
get { return _employee; }
set { _employee = value; }
}

// Tried this - but still does not work (it displays the empoyee when
// used as a column, but no sorting.
public string EmployeeName
{
get { return _employee.EmployeeName; }
}
}


Any suggestions? How can I sort on the column with the sub-object? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 29, 2006 4:43 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
To solve the sort/pagination problem in my gridviews I'm using ObjectDataSource with a helper object. I have a helper object for each domain object I display in a GridView, which implements the expected methods for update/delete including sorting and pagination.

hth,
radu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 29, 2006 6:00 am 
Newbie

Joined: Mon Mar 27, 2006 10:24 am
Posts: 8
Location: Malmoe, Sweden
Radu, could you please explain how you do this and maybe give us a sample?

Thanks
/Floda


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 29, 2006 8:56 am 
Newbie

Joined: Mon Aug 28, 2006 12:47 am
Posts: 6
Location: British Columbia, Canada
An example would be good. I've thought about using ObjectDataSource and providing a wrapper, but it seems like a lot of extra effort for a simple convenience. Is there a way to join in the hbm file and expose the property, even if you don't use it for insert and update (setting these to false)? I see that there are some property-ref, outer-join, etc attributes to the many-to-one node.

Radu, I'd appreciate a sample of a wrapper. I'm just curious to see how you expose the property. Thanks.


Top
 Profile  
 
 Post subject: I've dealt with sorting and paging
PostPosted: Tue Sep 05, 2006 9:33 pm 
Newbie

Joined: Fri Apr 14, 2006 3:04 pm
Posts: 10
Although you can roll your own paging and sorting, the complexities of doing so don't really justify the effort, imo.

I rolled GridViews with NHibernate the following way:

Create an object data source and gridview.
Set the gridview's datasourceid to the ControlID of the object datasource.
Set the object data source's select method and SortExpression (the parameter in the select method you will sort on)

The select method will need to do the sorting, and there are a couple of pecularities with how the GV will send the sort expression (it will append "ASC" or "DESC" as if it were a SQL datasource, even though you have it hooked up to an object data source).

How to sort? You will probably want to implement IComparable on your entity object. There are also other ways. Also, just a heads up...to sort on the "nested" properties, you will probably want to implement some type of solution using reflection so that on a GV column, you could enter something like "Object1.Object2.Property" for the SortExpression.

Maybe I'll post how to do the solution in more detail if there's enough interest...


Top
 Profile  
 
 Post subject: Using the ObjectDataSource with a helper Object?
PostPosted: Wed Sep 06, 2006 11:00 pm 
Newbie

Joined: Tue Sep 05, 2006 11:15 pm
Posts: 7
Radu & Cupcake, I found that you both have experience in using NHibernate with ObjectDataSource. I'm still working hard on that but not very successful. Could you please give us an example on using NHibernate with ObjectDataSource?

Up to now, I've experiement with it and got the following finding.
1. In ObjectDataSource, I need to specify the "DataObjectTypeName" in order to configure my Insert/Update/Delete method of the ObjectDataSource. Otherwise, I believe a list of parameters is needed. (I've not tried that.)
2. After completed step 1, I can use the design mode to edit the columns in GridView. I can also set the GridView to enable Edit/Delete in the design mode.
3. Here comes the problem. After I set the "DataObjectTypeName", my web page will raise an exception when trying to cast an ArrayList into a IList(Of objectName). The reason is my select function is expected to return a IList(Of objectName) while the List() function will return an IList. How can I solve this problem?

My own VB code making use of BaseDataAccess of Benjamin's tutorial.

Code:
    Public Function GetMaster() As IList(Of Master)
        Dim mgr As New BaseDataAccess

        Return mgr.Get(GetType(Master))
    End Function


mgr.Get will call the following function finally.

Benjamin C# code (By the way, is this your so-called helper function?)
Code:
        private IList GetByType(Type type)
        {
            IList items = null;
            ITransaction tx = null;

            try
            {
                tx = m_session.BeginTransaction();
                items = m_session.CreateCriteria(type).List();
                tx.Commit();

                return items;
            }
            catch (Exception ex)
            {
                if (tx != null) tx.Rollback();
                throw ex;
            }
        }


Sorry for off topic but I think Hpcd is also willing to see such example.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.