Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Validating data in DataRowChanging event

I have what seems to be a rather strange problem
in my VB.Net windows form.

I've got a few textboxes bound to a data table, and when the user navigates between records, I'm doing some validation in the RowChanging event of the data table.
In that event handler, if the validation rules aren't met,
I throw a ArgumentException - which, according to just
about any source out there (MSDN, books, sites, you name it), should cancel the navigation. It does, but it also kills the
app - apparently, it simply behaves as an uncaught exception.

Here's some pseudocode in case you need it:

---------------------------------
''Event handler:
AddHandler dt.RowChanging, AddressOf TheHandler
.......

Sub TheHandler(ByVal s As Object, ByVal e As
                                      DataRowChangeEventArgs)
        If  Validate()=False Then
                Throw New ArgumentException("Bad input")
        End If       
End Sub
--------------------------------

Thanks for any advice you might have.

- Mike.

Mike
Wednesday, September 10, 2003

You throw an Exception. You don't catch it. You're surprised when your app stops... as if an uncaught exception were thrown. How strange ;-).

How about calling CancelEdit() on the Row (ie, e.Row.CancelEdit())?

Duncan Smart
Wednesday, September 10, 2003

Well, the documentation explicitly says that this is how you
cancel the event... even more, if you do try to CancelEdit or EndEdit or RejectChange, you'll see an error message telling you something along the lines of "You cannot call canceledit from the rowchanging event; throw an exception instead".

My question might sound like it's coming from a complete newbie who's been programming for day or two, but this is
not the case.  It looks more and more like a framework bug.

Mike
Wednesday, September 10, 2003

Sorry Mike I was in a facetious mood the other day! In that case catch the exception. Is it a Save button or something that triggers the event?... or MoveNext or something. What I'd do would be catch an exception of my own making there:

Class DodgyDataException
  Inherits ApplicationException
  Public ReadOnly ColumnName As String
  Sub New(message As String, colName as String)
      MyBase(message)
      ColumnName = colName
  End Sub
End Class


Sub SaveBtn_Click(...) Handles ...
  Try
      BindingContent[...].EndCurrentEdit()
  Catch e As DodgyDataException
      MsgBox "Something wrong with " & e.ColumnName ...
  End Try
End Sub

Another thing to look into is the fact that row/column values have different versions - specifically DataRowVersion.Current and Proposed versions. I guess you could look ar the proposed version:
  x = e.Row("ColName", DataRowVersion.Proposed)
complain and set it it back to it's original version:
  e.Row("ColName", DataRowVersion.Proposed) = e.Row("ColName", DataRowVersion.Current)

HTH

Duncan Smart
Thursday, September 11, 2003

Catching the exception myself would of course work, but
that sort of defeats the purpose of the whole thing - I mean, instead of having one place where I'd do the
validation, I'd have to put try/catch's everywhere the event
is triggered: in navigation buttons, in Save, etc.  I might
as well write my own validation function and call it everywhere myself without any need for the rowchanging event handler at all.  It doesn't look like validating data in the event handler is of much help then.

Thanks for your input though.

Mike
Friday, September 12, 2003

*  Recent Topics

*  Fog Creek Home