catching exceptions SelfServicing

Posts   
 
    
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 29-Apr-2009 16:50:56   

I have trouble handlling exceptions. My code:


    Try
      Dim o As New StoreItemsEntity()
      With o
        .Fields("StoreItemId").ForcedCurrentValueWrite(storeItemId)
        .IsNew = False
        .StoreItemTypeId = submittedValues(StoreItemsFields.StoreItemTypeId.Alias)
        .CustomerId = submittedValues(StoreItemsFields.CustomerId.Alias)
        .StoreItemUniqueId = submittedValues(StoreItemsFields.StoreItemUniqueId.Alias)
        .StoreItem = submittedValues(StoreItemsFields.StoreItem.Alias)
        .StoreItemDeployable = submittedValues(StoreItemsFields.StoreItemDeployable.Alias)
        .StoreItemDescription = submittedValues(StoreItemsFields.StoreItemDescription.Alias)
      End With


      o.Save()
    Catch ex As System.Data.SqlClient.SqlException

      If ex.Message.Contains("Cannot insert duplicate key row in object") Then
        Dim msg As New LiteralControl("<div style='color:red;padding:3px 3px 3px 3px;'>Cannot insert duplicate key row in object. Make sure that the Item identification is unique to the customer<div>")
        rg.Controls.Add(msg)
      Else
        Throw New Exception(ex.Message, ex.InnerException)
      End If

    End Try

Regardless of the try/catch an exception is thrown deeper down in the code:


Server Error in '/InStoreChamp' Application.
--------------------------------------------------------------------------------

Cannot insert duplicate key row in object 'dbo.StoreItems' with unique index 'UC_StoreItems'.
The statement has been terminated. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.StoreItems' with unique index 'UC_StoreItems'.
The statement has been terminated.

Source Error: 


Line 703:       Protected Overrides Overloads Function UpdateEntity() As Boolean
Line 704:           Dim dao As StoreItemsDAO = CType(CreateDAOInstance(), StoreItemsDAO)
Line 705:           Return dao.UpdateExisting(MyBase.Fields, MyBase.Transaction)
Line 706:       End Function
Line 707:       


Source File: C:\Visual Studio 2008\Projects\myproject\myproject.DLL\EntityClasses\StoreItemsEntity.vb   Line: 705 


What am I doing wrong?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-Apr-2009 07:09:24   

Regardless of the try/catch an exception is thrown deeper down in the code

Could you please elaborate more on that?

David Elizondo | LLBLGen Support Team
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 30-Apr-2009 08:44:30   

daelmo wrote:

Regardless of the try/catch an exception is thrown deeper down in the code

Could you please elaborate more on that?

Since the exception is thrown in the generated code (snippet below), and not bubbled up, I don't know how to catch and handle the exception in the aspx code-behind file.

Line 703: Protected Overrides Overloads Function UpdateEntity() As Boolean Line 704: Dim dao As StoreItemsDAO = CType(CreateDAOInstance(), StoreItemsDAO) Line 705: Return dao.UpdateExisting(MyBase.Fields, MyBase.Transaction) Line 706: End Function

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Apr-2009 11:34:15   

What if you don't specify a type for the exception when catching?