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: [answered] Query for Null Values
PostPosted: Tue Feb 10, 2009 3:29 pm 
Newbie

Joined: Wed Jul 04, 2007 9:28 am
Posts: 9
Hi,
The code i have to work with, was written by a guy who is not at the company anymore so i hope this isnt to basic to ask for.

What i Want:
I want to query a Table for all Records Containing a NULL Value in a specific nvarchar field ( called "Number"). But when i go for it entering the method which sets up the criteria i get not a single record back. if i try it with an empty string, i get some result.. but absolutley not what i want. I Get Records with the Number field containing String values like "10" or "11".

the Methods for the criteria setup look like this:
I step into the first method when in debug mode, the hashtable contains the
key, value: "Number",null or "Number",""
no sortorder or maxresult set

Code:
        public override System.Collections.IList Search(Type type, System.Collections.Hashtable Properties, System.Collections.Hashtable SortOrder, int MaxResults) {
            try
            {
                NHibernate.ISession _s = NHibernateHttpModule.CurrentSession;

                ICriteria crit = _s.CreateCriteria(type);

                // set the criterias
                foreach (object key in Properties.Keys)
                {
                    object val = Properties[key];
                    if (Properties[key] is string)
                    {
                        val = Properties[key].ToString();
                        if (!val.ToString().Contains("%")) val = string.Format("%{0}%", val);
                    }

                    crit.Add(Expression.Like(key.ToString(), val));
                }
                // set sort order
                foreach (object key in SortOrder.Keys)
                {
                    crit.AddOrder(new Order(key.ToString(), (bool)SortOrder[key]));
                }
                // set maxresults
                if (MaxResults > 0)
                {
                    crit.SetMaxResults(MaxResults);
                }
                return crit.List();

            }
            catch (Exception ex) {
                throw ex;
            }
        }


        public override System.Collections.IList Search(Type type, System.Collections.Hashtable Properties)
        {
            try
            {
                NHibernate.ISession _s = NHibernateHttpModule.CurrentSession;


                ICriteria crit = _s.CreateCriteria(type);
                foreach (object key in Properties.Keys)
                {
                    crit.Add(Expression.Like(key.ToString(), Properties[key]));
                }
                return crit.List();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       
        public override System.Collections.IList Search(Type type, string PropertyName, object PropertyValue, int MaxResults)
        {
            try
            {

                Hashtable Properties = new Hashtable();
                Properties.Add(PropertyName, PropertyValue);
                return this.Search(type, Properties, new Hashtable(), MaxResults);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



Mapping documents:
<class name="Hobex.CRM.Invoice, Hobex.CRM.Models" table="TerminalInvoice">
<id name="Id" type="Int32" column="TerminalInvoiceID" access="field.pascalcase-m-underscore">
<generator class="identity" />
</id>
<property name="Number" column="InvoiceNumber" type="String" />
<property name="State" column="InvoiceStatusID" type="Int32" />
<property name="CreateDate" column="CreateDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />
<property name="CreateUser" column="CreateUser" type="String" />
<property name="ModifyDate" column="ModifyDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />
<property name="ModifyUser" column="ModifyUser" type="String" />
<property name="IntendedPurposeLineOne" column="IntendedPurpose1" type="String" />
<property name="IntendedPurposeLineTwo" column="IntendedPurpose2" type="String" />
<property name="IntendedPurposeLineThree" column="IntendedPurpose3" type="String" />
<property name="Information" column="InfoText" type="String" />
<property name="EnquiryDate" column="EnquiryDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />
<property name="Expenses" column="Spesen" type="Decimal" />
<property name="Amount" column="Amount" type="Decimal" />
<property name="Date" column="InvoiceDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />
<property name="Generated" column="Generated" type="Boolean"/>
<property name="Deposited" column="Deposited" type="Boolean"/>
<property name="CalculateTax" column="CalculateTax" type="Boolean"/>
<property name="Link" column="Link" type="String" />
<property name="DateFrom" column="DateFrom" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />
<property name="DateTo" column="DateTo" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" />
<many-to-one name="Member" column="MemberID" class="Hobex.CRM.Member, Hobex.CRM.Models" />
<many-to-one name="Terminal" column="TerminalID" class="Hobex.CRM.Terminal, Hobex.CRM.Models"/>
<many-to-one name="Kind" column="InvoiceKindID" class="Hobex.CRM.InvoiceKind, Hobex.CRM.Models" />
<many-to-one name="Tax" column="UstId" class="Hobex.CRM.Tax, Hobex.CRM.Models" />
<bag name="Details">
<key column="TerminalInvoiceId" />
<one-to-many class="Hobex.CRM.InvoiceDetails, Hobex.CRM.Models" />
</bag>
</class>

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

Thanks in advance
AS


Last edited by Alter Schwede on Wed Feb 11, 2009 5:34 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 4:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can't check for null with normal operators .. you have to explicitly to a "is null" or "is not null" comparison. In case of "" you have something like "col like '%%'" which will probably return all rows.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:30 am 
Newbie

Joined: Wed Jul 04, 2007 9:28 am
Posts: 9
Hi Wolli,

Thanks for the hint, it works fine now!

hmm how can I mark this thread as "answered"?

bye
as


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:34 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
There's nothing like "answered" here. You can only rate answers.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:36 am 
Newbie

Joined: Wed Jul 04, 2007 9:28 am
Posts: 9
ok, I rated your answer
thanks again
cyu
as


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.