Validation of data in Visual C# RRS feed

  • Question

  • Hi,

    I'm writing a database app in Visual C#, using some of the drag-and-drop tools to create bound forms. I want to validate data before it is written to the database (preferably as soon as the user leaves the field) - things like changing a field to all uppercase, and ensuring that fields comply with the constraints on the SQL database.

    Just what is the best way of doing this? Where exactly should the code go?
    Friday, July 20, 2007 9:22 AM

All replies

  • A good place to hook this in is in a control's Validating and Validated events. The former allows you to perform your validation and, if you want, prevent the user from leaving the control until they put in a valid value. Be careful though, it's a great way to wind users up.

    You shouldn't put the actual validation code in the control's event though, just the call to it.

    This is just one approach of course, there are many others.

    Sorry if this seems like a plug, but my Foundation product generates the objects for you and has built-in validation that checks that all non-nullable columns have values and that none of the strings will exceed the max length the database column can hold without truncation. It gives you this automatically and gives you an easy way to add your own validation. However it doesn't automatically do this when the user leaves a control. You may still find it useful however.


    Monday, July 30, 2007 7:41 PM