tags:

views:

120

answers:

2

Hello, I have a system with 3 layers and I am using LINQ-to-SQL to persist.

I want to validate if the description field of my object is empty. I am using partial classes and the method OnValidate(). If the field is empty, it throws an exception.

Is this correct?

What do I do after the exception to not close the form, letting the user continue working?

Thank you very much

Sorry for my poor english

A: 

How you handle it exactly depends on your what the application is (e.g. ASP.Net, MVC, WebForms), but your exception should return a list of the validation errors so that your view can handle them gracefully, e.g. display them on the form.

There are some good frameworks for handling this, such as XVal if it is a web application.

Turnkey
In C# / WinForms with 3 layers, when you have an exception, you can catch the exception, see what happens, etc, but after that, the program keep running and get closed?, right?. It is possible to catch the exception, show throw up to UI, show a messagebox (all this things I can do) and NO quit the program?? (this final step is my problem)
Mark Comix
Yes, there is no reason to quit the program in this case. Since you have caught and handled the exception you can simply show the errors in a label on the form. The form should remain running. For example the message might tell the user to correct the error and submit the form again.
Turnkey
A: 

look into this link which shows bunch of tricks about how to override onValidate() and other linq tricks

lakhlaniprashant.blogspot.com