in

CodeSmith Community

Your Code. Your Way. Faster!

Help with Data Exception Handling

Last post 03-04-2008 10:03 PM by wkampas. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 03-04-2008 3:39 PM

    • wkampas
    • Top 500 Contributor
    • Joined on 05-30-2006
    • Posts 9
    • Points 135

    Help with Data Exception Handling

    Please Help! 

    I've spent the entire day searching the forums for a simple how-to, with no luck.  Any help with this would be greatly appreciated.

    I'm using the EntityGridView with a strongly-typed datasource and displaying the Delete button on each row.  When I try to delete a row that would violate a referential integrity constraint, an exception bubbles up to the UI with the usual error message about foreign keys etc.  This message is fine for me, because I know what it means.  However, for end-users of the application, I want to provide a nice message.

    My question is: how (and where) do I trap the exception, and change the message that eventually gets displayed for the end-user?

    Things I've already tried are:

    • Setting "OnDeleted" and OnDeleting" for both the EntityGridView, and the DataSource - My code-behind doesn't ever seem to be hit while debugging.
    • I can see the exception propagating through the ExecuteDelete method in BaseDataSource.cs, but it doesn't seem like this would be the appropriate place to do something about it.

    Both the "How" and "Where" are important to me as I am new to C#, and all the layers in the NetTiers are very confusing to me.  I'm guessing that there must be some place where I can override the default behavior, but I don't know where or how to do it.

    My generated code is using Enterprise Library 3.1 and nightly build 704 of NetTiers, and I have all standard stuff that writes the exception details to the log.exclude file. 

    Thanks in advance...

    • Post Points: 35
  • 03-04-2008 4:48 PM In reply to

    • fweeee
    • Top 25 Contributor
    • Joined on 08-08-2007
    • Posts 164
    • Points 4,103

    Re: Help with Data Exception Handling

    I'm not an expert, so many someone else can give you a better answer, but I'll breifly explain how I go about this.

    I add validation rules to the entity in the service file. That is, I override the standard get routines, and add validation rules which only kick in if the entity is marked for deletion. I add it at the service layer, as usually the rules around deleting a record involve other records/tables in the database, which can only be accessed at the service layer.

    Before I delete a record, I mark it for deletion, then validate it, and check that the record is valid. If it is not (probably due to some delete rule not being happy), I show that message as an error, and dont attempt to actually delete.

    I can be a bit more specific if you think this is the path you want to go down. 

    • Post Points: 60
  • 03-04-2008 5:56 PM In reply to

    • wkampas
    • Top 500 Contributor
    • Joined on 05-30-2006
    • Posts 9
    • Points 135

    Re: Help with Data Exception Handling

    fweeee,

    Thanks so much for the quick response - and I assure you that your expertise far exceeds my own, having just started using C# and NetTiers.

    My question is more focused on trapping and dealing with the exception, than validation.  I realize that in this instance I could validate that it was OK to delete the row, and thereby avoid the exception, but that doesn't work for me as a general solution.  Some of my tables have multiple complex relationships - and there are many reasons that a row deletion (or insert, or update) could fail.  If I were to take the validation approach, I would have to reimplement my whole database design in validation code.  That's a problem for 3 reasons:

    1. Writing a boatload of validation code would take too long.
    2. Conceptually, my app would then be tightly coupled with my database design, and a maintenance nightmare would result.
    3. As you said, the rules around record deletion involve other records/tables in the database, and implementing a bunch of validations would create a huge performance hit (as many round-trips to the DB would be required just for the validation).

    So I would prefer to let the database enforce its own referential integrity rules, and just trap what it doesn't allow.  The problem is that I can't just let cryptic error error messages bubble up to the UI and blow up in the end-users' faces.

    As a neophyte in both C# and NetTiers, I only have a vague conceptual understanding of how the NetTiers pieces fit together.  I've read all the NetTiers documentation, but only understand about half of it because of my inexperience with C#.  I can follow the execution path while debugging, but it usually leads me to a dead end.  Here's an example that applies to what I'm trying to do:

    I have a database table called Employees.  NetTiers has generated all these things that relate my Employees table:

    • a Components project that contains EmployeesService.cs and EmployeesServiceBase.generated.cs
    • a Data project that contains EmployeesProviderBase.cs and EmployeesProviderBase.generatedCore.cs
    • a Data.SQLClient project that contains SqlEmployeesProvider.cs and SqlEmployeesProviderBase.generated.cs
    • An Entities project that contains Employees.cs, EmployeesBase.generated.cs, and IEmployees.cs
    • A web project that contains EmployeesDataSource.cs

    So the questions remain; where in all these files would the best place to trap a data exception (like a foreign-key constraint), how do I trap it, and once I've trapped the exception how do I change the content of the message that gets returned to the UI (either by changing the text of the error, or marking the exception "handled" and sending sending out my own message)?

    This can't be that diffucult.  Isn't this something that everyone would have to do? 

    If anyone can help me with this, I'll be forever in your debt...

    -Walt

     

     

     

    • Post Points: 5
  • 03-04-2008 10:03 PM In reply to

    • wkampas
    • Top 500 Contributor
    • Joined on 05-30-2006
    • Posts 9
    • Points 135

    Re: Help with Data Exception Handling

    Here's an update on my questions...

    I added the following to my EmployeesService.cs code, and it seemed to do what I wanted/expected.  I still don't know whether this is a reasonable/appropriate way to go about this or not.  Any input is welcome.

    ***

    public override bool Delete(Employees entity)

    {

    try

    {

    return base.Delete(entity);

    }

    catch (Exception e)

    {

    if (e.GetType().Name == "SqlException")

    {

    System.Data.SqlClient.
    SqlException z = (System.Data.SqlClient.SqlException)e;switch (z.Number)

    {

    case 547: //WGK - Foreign key constraint violation

     

    throw new DataException(String.Format("You cannot delete {0} {1}, who has associated deposit records.", entity.EmployeeFirstName, entity.EmployeeLastName));

    default:

    throw;

    }

    }

    throw;

    }

    }

    • Post Points: 5
Page 1 of 1 (4 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems