-->
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.  [ 5 posts ] 
Author Message
 Post subject: Datagrid binding problem
PostPosted: Tue Oct 31, 2006 6:05 am 
Newbie

Joined: Wed Oct 25, 2006 10:47 pm
Posts: 2
Greetings all,
I'm new to NHibernate and currently seeking some help here. The problem is that I have been trying to bind a datagrid from the method ListBlogsAndRecentItems() as listed below.
Code:
public IList ListBlogsAndRecentItems()
      {
         IList result = null;

         using (ISession session = _sessions.OpenSession())
         using (ITransaction tx = session.BeginTransaction())
         {
            IQuery q = session.CreateQuery(
               "select blog.Id, blog.Name, blogItem.Text from    Blog as blog " +
               "join fetch blog.Items as blogItem " +
               "where blogItem.DateTime > :minDate"
               );
   
            DateTime date = DateTime.Now.AddMonths(-1);
            q.SetDateTime("minDate", date);
               
            result = q.List();
            tx.Commit();
         }

         return result;
      }

When I have the datagrid autogenerate the columns I get this as the result in my datagrid:
Code:
Rank IsReadOnly IsSynchronized IsFixedSize Length LongLength
1 False False True 3 3


I basically want to return a resultset comparable to this SQL statement:
Code:
select [blog_id], [Name], [text] from blog_items, blogs where blogs.blog_id = blog_items.blog_id


I have searched the fourms and basically read all topics regarding datagrid binding with NHibernate and have not been able to solve my problem. I've also read the NHibernate documentation on HQL, and Collection mapping with no success in resolving this issue. I'm at my wits end here and would appreciate some help. So far I like what I have seen and would like to try to do more with it so I can justify using it at work. Eventually I would like to use Template Columns instead of having the datagrid columns autogenerate. Thanks again for any help



Hibernate version:
1.0.2.0

Name and version of the database you are using:
Microsoft SQL 2000

ASP.NET Framwork:
1.1

Mapping documents:
Blogs Mappings
Code:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
    assembly="BlogSample" namespace="BlogSample.AppDomain">

    <class
        name="Blog"
        table="BLOGS"
        lazy="true">
       
        <id
            name="Id"
            column="BLOG_ID">
           
            <generator class="native"/>
           
        </id>
       
        <property
            name="Name"
            column="NAME"
            not-null="true"
            unique="true"/>
           
        <bag
            name="Items"
            inverse="true"
            lazy="true"
            order-by="DATE_TIME"
            cascade="all"
            fetch="join">
           
            <key column="BLOG_ID"/>
            <one-to-many class="BlogItem"/>
           
        </bag>
        </class>
   
</hibernate-mapping>


BlogItem mappings
Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
    assembly="BlogSample" namespace="BlogSample.AppDomain">
   
    <class
        name="BlogItem"
        table="BLOG_ITEMS"
        dynamic-update="true">
       
        <id
            name="Id"
            column="BLOG_ITEM_ID">
           
            <generator class="native"/>
           
        </id>
       
        <property
            name="Title"
            column="TITLE"
            not-null="true"/>
           
        <property
            name="Text"
            column="TEXT"
            not-null="true"/>
           
        <property
            name="DateTime"
            column="DATE_TIME"
            not-null="true"/>
           
        <many-to-one
         name="Blog"
            column="BLOG_ID"
            not-null="true"
            />
           
    </class>
   
</hibernate-mapping>

Blog.cs
Code:
using System;
using System.Collections;

namespace BlogSample.AppDomain
{
   /// <summary>
   /// Summary description for Blog.
   /// </summary>
   public class Blog
   {
      private long _id;
      private string _name;
      private IList _items;

   
      public virtual long Id
      {
         get { return _id; }
         set { _id = value; }
      }
       
      public virtual IList Items
      {
         get { return _items; }
         set { _items = value; }
      }
       
      public virtual string Name
      {
         get { return _name; }
         set { _name = value; }
      }
   }
}

BlogItem.cs
Code:
using System;

namespace BlogSample.AppDomain
{
   /// <summary>
   /// Summary description for BlogItem.
   /// </summary>
   
      public class BlogItem
      {
         private long _id;
         private DateTime _dateTime;
         private string _text;
         private string _title;
         private Blog _blog;

         public virtual Blog Blog
         {
            get { return _blog; }
            set { _blog = value; }
         }

         public virtual DateTime DateTime
         {
            get { return _dateTime; }
            set { _dateTime = value; }
         }

         public virtual long Id
         {
            get { return _id; }
            set { _id = value; }
         }

         public virtual string Text
         {
            get { return _text; }
            set { _text = value; }
         }

         public virtual string Title
         {
            get { return _title; }
            set { _title = value; }
         }
      }
   
}


Top
 Profile  
 
 Post subject: Hope this helps
PostPosted: Tue Oct 31, 2006 5:36 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear dseed62,

i've faced the same problem .. but before answering, i'm gonna be sure of the question ..
Did u mean that u can't get the property values in the retrieved IList?
If so, u can create a class at runtime suchh as :
"Select new blog(blog.id,blog.name,...) from .. "
when u do that, u can get property values returned and properly binded to your datagrid (gridview .)

Regards,
Jojo

[If my answer helped you, please rate it ][/i]

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Problem Solved
PostPosted: Wed Nov 01, 2006 12:46 am 
Newbie

Joined: Wed Oct 25, 2006 10:47 pm
Posts: 2
jojorico,
Thank you so much that did the trick! Oh man I spent 1 week trying to figure this out. I'm beginning to like NHibernate more and more. Well, here is the solution for those true newbies like myself. I know this may not be right but I will still be fiddling around with the code. Any input is appreciated.

Here is the modified method ListBlogsAndRecentItems()
Code:
public IList ListBlogsAndRecentItems()
      {
         IList result = null;

         using (ISession session = _sessions.OpenSession())
         using (ITransaction tx = session.BeginTransaction())
         {
            IQuery q = session.CreateQuery(
               "select new BlogItem(blog, blogItem.Text) from BlogItem blogItem " +
               "join fetch blogItem.Blog blog " +
               "where blogItem.DateTime > :minDate"
               );
   
            DateTime date = DateTime.Now.AddMonths(-1);
            q.SetDateTime("minDate", date);
               
            result = q.List();
            tx.Commit();
         }

         return result;
      }


Be sure to create the proper constructors in BlogItem.cs
Code:
using System;

namespace BlogSample.AppDomain
{
   /// <summary>
   /// Summary description for BlogItem.
   /// </summary>
   
      public class BlogItem
      {
         private long _id;
         private DateTime _dateTime;
         private string _text;
         private string _title;
         private Blog _blog;

         public virtual Blog Blog
         {
            get { return _blog; }
            set { _blog = value; }
         }

         public virtual DateTime DateTime
         {
            get { return _dateTime; }
            set { _dateTime = value; }
         }

         public virtual long Id
         {
            get { return _id; }
            set { _id = value; }
         }

         public virtual string Text
         {
            get { return _text; }
            set { _text = value; }
         }

         public virtual string Title
         {
            get { return _title; }
            set { _title = value; }
         }

         public BlogItem(Blog blog, string text)
         {
            Blog = blog;
            Text = text;
         }

         public BlogItem()
         {}
      }
   
}


Bind your grid and you should be good to go!
Code:
<asp:datagrid id="dgrdBlog" style="Z-INDEX: 109; LEFT: 400px; POSITION: absolute; TOP: 128px"
            runat="server" AutoGenerateColumns="False">
            <Columns>
               <asp:TemplateColumn HeaderText="Id">
                  <ItemTemplate>
                     <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "blog.Id") %>'>
                     </asp:Label>
                  </ItemTemplate>
                  <EditItemTemplate>
                     <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "blog.Id") %>'>
                     </asp:TextBox>
                  </EditItemTemplate>
               </asp:TemplateColumn>
               <asp:TemplateColumn HeaderText="Name">
                  <ItemTemplate>
                     <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "blog.Name") %>' ID="Label4" NAME="Label4">
                     </asp:Label>
                  </ItemTemplate>
                  <EditItemTemplate>
                     <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "blog.Name") %>' ID="Textbox4" NAME="Textbox4">
                     </asp:TextBox>
                  </EditItemTemplate>
               </asp:TemplateColumn>
               <asp:TemplateColumn HeaderText="Text">
                  <ItemTemplate>
                     <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' ID="Label5" NAME="Label5">
                     </asp:Label>
                  </ItemTemplate>
                  <EditItemTemplate>
                     <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' ID="Textbox5" NAME="Textbox5">
                     </asp:TextBox>
                  </EditItemTemplate>
               </asp:TemplateColumn>
            </Columns>
         </asp:datagrid>


Last edited by dseed62 on Wed Nov 01, 2006 2:00 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 1:48 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear dseed62,

i know what does it mean to spend 1 week waiting for a reply ...
i faced that same problem of yours ... and still ..

I'm glad that my post helped :)

Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 8:39 am 
Beginner
Beginner

Joined: Fri Oct 20, 2006 8:02 am
Posts: 36
Hi, I was looking your HQL, I think it could be more simple.

try this:

Code:
IQuery q = session.CreateQuery("select from BlogItem blogItem where blogItem.DateTime > :minDate");
q.SetDateTime("minDate", DateTime.Now.AddMonths(-1));


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