Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

How to do Data Binding Validation?

The one issue which keep me from using data binding is how to do validation.
What I want to do:
- validate user input

if it's OK then:
- update business object(s)

if it's not (OK) then:
- output the failed value to user
- output a message explaining the failure
- do not update business object(s) with failed values.

And I want this to happen for all failures, not just the first encountered.

Thomas Eyde
Friday, September 20, 2002

ASP.NET or Window.Forms?

Dunc
Friday, September 20, 2002

Actually, both, if you care. But most WinForms. Thanks.

Thomas Eyde
Friday, September 20, 2002

Data binding in .NET is not like how it was in, say previous versions of VB. Controls are bound to a *disconnected* DataSet and no changes are sent back to the database until you explicitly call DataAdapter.Update() or whatever.

So you can do your checks by having your code inspect the DataSet before it ships the changes back to the back end tiers.

Dunc
Friday, September 20, 2002

You can validate user input in Binding.Parse:
AddHandler myTextBox.DataBindings(0).Parse, AddressOf ValidateTextBox

You can also validate in KeyPress or similar events.

I do a validation on the interface (Binding.Parse), then I send the data to the business rules layer. There, I validate everything again, to be sure invalid data can't be inserted into the database.

Etienne Charland
Saturday, September 21, 2002

Thank you all for your feedback. How do you validate in Binding.Parse when the expected data type is DateTime, but the user entered data is a string like "qoweiurty"? The data set would expect a DateTime (or some data provider equivalent), so I can't just bang in what ever into it.

And in Web Forms I would need to write back the user entered value into the gui control, at least when ViewState is turned off. How do I keep track of the user entered value so I can write it back?

Thomas Eyde
Saturday, September 21, 2002

Understand the flow:

User -> Control -> Binding -> DataSet... and then, when they click "Save": DataAdapter -> data source.

By hooking up at the Binding stage allows you to pre-empt what goes into their DataSet.

Dunc
Monday, September 23, 2002

*  Recent Topics

*  Fog Creek Home