The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Possible Defect - Exception not being thrown from OnQueryLoaded

Last post 12-19-2007, 7:30 PM by David.Parsons. 11 replies.
Sort Posts: Previous Next
  •  09-28-2007, 8:36 PM 5372

    Possible Defect - Exception not being thrown from OnQueryLoaded

    Hi Guys,

    Using ES version: 2007.0.913.0

             MyGeneration version: 1.2.0.7

             Oracle XE (10g free edition)

    Attempting to load a single entity, but two or more are being returned from the database. I have wrapped the call to Query.Load in a Try-Catch block, but the 'An entity can only hold one record of data' exception is not being propagated up to the Load call. The call to Me.PopulateEntity(table) in OnQueryLoad, is failing with an Unhandled Exception and the RowCount check is not executed in order to throw the exception.

    I did not use the 'Custom Base Class' option in generating my class, so it inherits from esEntity. The following is the code I am trying:

     

    Code:
     _543Transaction = New Transaction
    
            _543Transaction = SetCommonWhereConditions(_543Transaction)
    
            With _543Transaction.Query
                .Where(.AccountNo.Equal(acct))
                .Where(.SubAccountNo.Equal(subAcct))
                .Where(.TransTypeCd.Equal("Z"))
                Try
                    If .Load() = False Then
                        _543Transaction.AddNew()
                        _543Transaction.AccountNo = acct
                        _543Transaction.SubAccountNo = subAcct
                        _543Transaction.TransactionDate = CType("01/01/2007", Date)
                        _543Transaction.TransactionAmt = 5
                        _543Transaction.TransTypeCd = "Z"
                        _543Transaction.Save()
                    Else
                        _543Transaction.TransactionAmt = _543Transaction.TransactionAmt.Value + 5
                        _543Transaction.Save()
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.ToString)
                End Try
    
            End With

          

    Code:

    'The Unhandled Exception occurs on the  Dim dataFound as Boolean = Me.PopulateEntity(table) line

    Protected Function OnQueryLoaded(ByVal table As DataTable) As Boolean
                Dim dataFound as Boolean = Me.PopulateEntity(table)

                If Me.RowCount > 1 Then
                    throw New Exception("esTransaction can only hold one record of data")
                End If
               
                Return dataFound
            End Function   

    What am I Missing something?  
     

  •  09-29-2007, 5:22 AM 5376 in reply to 5372

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    I believe this is the issue. A collection can hold many records, a single entity can hold only 1 row as it represents only a single row. You're Query.Load() is returning more than one record from the database, thus the error. Here's what you can do, run the same query on one of your TransactionCollections and see how many rows come back, if it's more than 1 you either need to adjust the query or use a collection.



    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  09-29-2007, 9:17 AM 5381 in reply to 5376

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    Thanks for the reply Mike. I understand what you are saying, but in this instance I would rather use a single entity. The case when two rows would be returned is very rare, but it is a situation that needs to be handled gracefully so that the user can be warned that there is bad data in the database.

    I don't understand why the exception is not caught in the PopulateEntity(table) method?

    Why have the RowCount check if the code is never executed? It would be nice to be able to catch the exception on Query.Load so it can be handled properly.

  •  09-29-2007, 9:58 AM 5382 in reply to 5381

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    RowCount comes off of esEntityData which both esEntity and esEntityCollection inherit from. True, RowCount can never be more than one on an esEntity based object but there's no harm in that. We do throw the exception to warn you that your query returned more than one row which doesn't really mean there is bad data in the database, it just means your query returned more than one row. I guess the thing is when you decide to use the single entity to perform a query you need to be sure you're fetching just one row (but we throw the exception to make sure your aware that it didn't). I feel like our mechanism worked well in this case, it let you know that more than one row was returned.

    You should be able to catch that exception?  


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  09-29-2007, 12:51 PM 5383 in reply to 5382

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    I am not able to catch the exception, that is the problem...

    I get an UnhandledException on the call to PopulateEntity(table), therefore the exception is not propagated back up the stack to the Query.Onload call.

    The throw New Exception("esTransaction....line is not executed because of the UnhandledException. I could wrap the call to Me.PopulateEntity in a Try Catch block, but that is in the generated class and would be overwritten when regening the class. 

  •  09-30-2007, 5:11 AM 5394 in reply to 5383

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    Ahh, gotcha, yes, thank you for hanging in there. This is something we can fix, thanx.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  10-10-2007, 3:51 PM 5774 in reply to 5394

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    Okay, I went to fix this issue, ran this code and ...

     

    Code:
    try
    {
        Employee emp = new Employee();
        emp.Query.Load();
    }
    catch (Exception ex)
    {
        string s = ex.Message;
    }
     

    It caught the "An Entity can only hold 1 record of data". Perhaps I don't understand the issue after all?


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  10-12-2007, 5:53 AM 5813 in reply to 5774

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    The exception looks to be in his OnQueryLoaded function not the try catch block you tested. I'm not sure what event the OnQueryLoaded function is handling though.

  •  10-12-2007, 3:47 PM 5832 in reply to 5813

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    Hmmm...strange. I get different results. Below is the generated code for the OnQueryLoaded function of the Entity class. Note that the OnQueryLoaded function contains no Try Catch block and that the call to PopulateEntity(table) is where the exception occurs. It seems the RowCount check is being made in the PopulateEntity function and the exception being thrown from there as well. Using EntitySpaces.Core version 2007.0.913.0

     

    Code:
    Protected Function OnQueryLoaded(ByVal table As DataTable) As Boolean
    			Dim dataFound as Boolean = Me.PopulateEntity(table)
    
    			If Me.RowCount > 1 Then
    				throw New Exception("esGstCodeDesc can only hold one record of data")
    			End If
    			
    			Return dataFound
            End Function
     
  •  12-19-2007, 1:40 PM 7278 in reply to 5832

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    This is still a problem for me and other developers (not to mention our users).

    I peeked into the EntitySpaces.Core  - PopulateEntity - method and noticed that RowCount is checked and an Exception thrown if RowCount > 1.

    The generated class OnQueryLoaded method calls PopulateEntity and has no way of handling the exception thrown...this is why I get the Unhandled Exception and all the problems that arise because of it.

    This seems like an easy fix that would really help out our users.

    It is not a problem of me using a single entity when I should be using a collection, our system sometimes has times when more than one row is returned when expecting only one, and we need to know when this happens to deal with it appropriately.

    Thanks,

    Pete 

  •  12-19-2007, 1:48 PM 7279 in reply to 7278

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    In your last post you mentioned the Try Catch block caught the exception as expected when Query.Load was executed. Are you running the test from developement? I am not able to catch the exception nor the two other developers working on the same project. What could be the difference?
  •  12-19-2007, 7:30 PM 7283 in reply to 7279

    Re: Possible Defect - Exception not being thrown from OnQueryLoaded

    As to the difference, Mike has the source code, has the projects in his solution, and is referencing the projects. You are referencing just the compiled assemblies. Fortunately, this is just an inside VS while debugging issue. It does not happen in Release mode, it does not happen in Debug mode if you "Start without debugging", and will not occur outside VS when running the executable from either \bin\Debug or \bin\Release.

    Currently, you have 3 options until we get a fix out:

    1. Just click "Continue" in the debugger, and your Try/Catch block will be hit.
    2. Go to Tools -> Options -> Debugging -> General, and un-check "Enable Just My Code".
    3. Make a simple template change, and regenerate.

    For the EntitySpaces -> C# -> Generated -> EntitySpaces - esEntity(C#) template, add the attribute line above line 491 so it looks like this:

    Code:
    [System.Diagnostics.DebuggerNonUserCode]
    protected bool OnQueryLoaded(DataTable table)

    For the EntitySpaces -> VB -> Generated -> EntitySpaces - esEntity(VB.NET) template, add the attribute line above line 474 so it looks like this:

    Code:
    <System.Diagnostics.DebuggerNonUserCode> _
    Protected Function OnQueryLoaded(ByVal table As DataTable) As Boolean

    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML