Aggregate Function Max

Posts   
 
    
normanlc
User
Posts: 62
Joined: 10-Jan-2007
# Posted on: 16-Mar-2009 05:20:11   

I've searched and found a couple entries, but one was from 2005 and not applicable today (it included PredicateFactory)...

I'm working a on maintenance mess from another coder back some four years ago - he obvisously didn't know much about database design, anyway that's what I have to deal with...

Database: Access 2003

There is a table (you'll see he's old school just by naming convention and there is nothing wrong with "old school")...

Table: assigned_maintenance_report_number

Columns:


maintenance_report_num, Number, Long Integer
rma_num, Number, Long Integer
repair_tag_num, Number, Long Integer
maintenance_report_type, Text, 255,
client_company, Text, 255,
assigned_date, Date/Time, Short Date
assigned_by_id, Number, Long Integer
maintenance_report_id, AutoNumber, Long Integer

There is a dialog app to create assign Maintenance Report Numbers that I'm re-writing as some things have change. Anyway, I'm using LLBLGen Pro 2.6 with the build released in October 2008...

I'm using Self-Service build for the DLL as it is quick and I need to get this done yesterday (of course)...

In the AssignedMaintenanceReportNumberEntity class I've created a static method to get the next MR Number, create a row with the new data and then return the number so I can display it to the user...


      public static Int32 GetMaintenanceReportNumber(String report_type, String cubic_rma_num,
         String cubic_repair_tag, String transit_agency, Int32 technician_id)
      {
         AssignedMaintenanceReportNumberCollection collection = new AssignedMaintenanceReportNumberCollection();
         collection.GetScalar(AssignedMaintenanceReportNumberFieldIndex.MaintenanceReportNum, AggregateFunction.Max);
         if (collection.Count > 0)
         {
            AssignedMaintenanceReportNumberEntity amr_entity = new AssignedMaintenanceReportNumberEntity();
            amr_entity.AssignedById = technician_id;
            amr_entity.AssignedDate = DateTime.Now;
            amr_entity.ClientCompany = transit_agency.Trim();
            amr_entity.CubicRepairTagNum = (cubic_repair_tag == null || cubic_repair_tag.Trim().Length == 0) ? (Nullable<Int32>)null : Convert.ToInt32(cubic_repair_tag);
            amr_entity.CubicRmaNum = (cubic_rma_num == null || cubic_rma_num.Trim().Length == 0) ? (Nullable<Int32>)null : Convert.ToInt32(cubic_rma_num);
            amr_entity.MaintenanceReportNum = (collection[0].MaintenanceReportNum + 1);
            if (amr_entity.Save(true) == true) { return amr_entity.MaintenanceReportNum; }
            else { return 0; }
         }
         else { return 0; }
      }

That code returns 0 rather than the Maintenance Report Number because the

collection.Count IS ALWAYS 0

I've search for a couple hours trying to fine in documents samples us using the aggregate function MAX in such a way. I found on example in the forum and it's used above in the code.

Can someone advise what this knuckle head is doing wrong?

Thanks in advance!

Norman

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 16-Mar-2009 08:54:12   

The GetScalar() returns the value being queried, so you should do the following:

Int32 maxNum = (Int32)collection.GetScalar(AssignedMaintenanceReportNumberFieldIndex.MaintenanceReportNum, AggregateFunction.Max);
normanlc
User
Posts: 62
Joined: 10-Jan-2007
# Posted on: 16-Mar-2009 18:18:33   

Walaa wrote:

The GetScalar() returns the value being queried, so you should do the following:

Int32 maxNum = (Int32)collection.GetScalar(AssignedMaintenanceReportNumberFieldIndex.MaintenanceReportNum, AggregateFunction.Max);

Thank you very much (Slaps forehead)...

Norman