Database Access with Visual Basic .Net, 3rd edition - problem in example code, any ideas?

  • Thread starter Thread starter Bob of the West
  • Start date Start date
B

Bob of the West

Hi all,
Anyone have this book? I've run into an example that doesn't work, and
can't relax until I understand what's wrong!

Listing 1.4, p45 ( see below )
In the book it says 'If e.Row.Item "FirstName") = "" 'etc.

But the downloadable code says 'If e.Row.Item("FirstName").length = 0

Neither seem to work. I get "An unhandled exception of type
'System.Data.DeletedRowInaccessibleException' occurred in
system.data.dll Additional information: Deleted row information cannot
be accessed through the row."

Anyone already figured it out? There's no 'errata' section on the
publishers web site ( though you can download the source code )

Best wishes,
P

This is the subroutine:

Private Sub SqlDataAdapter1_RowUpdating(ByVal sender As Object, ByVal
e As System.Data.SqlClient.SqlRowUpdatingEventArgs) _
Handles SqlDataAdapter1.RowUpdating
'this line is in the ownload code
'If e.Row.Item("FirstName") = "" Or e.Row.Item("LastName") =
"" Then
'this line is in the book
If e.Row.Item("FirstName").length = 0 Or
e.Row.Item("LastName").length = 0 Then
MsgBox("Changes not Saved;Customer must have a first and
last name.")
e.Status = UpdateStatus.SkipCurrentRow
e.Row.RejectChanges()
End If
End Sub
 
Hi Bob,

Let I first say that I find this the most crazy place to test a value, this
can be in a class far away from your UI. But than a solution for your
problem. (Let say I give you the solution while I am protesting)

I think that if you insert this, it will go (and to do it better I also
inserted those ToString

If e.Row.RowState = DataRowState.Modified Then
If e.Row.Item("FirstName").ToString.length = 0 Or
e.Row.Item("LastName").ToString.length = 0 Then
MsgBox("Changes not Saved;Customer must have a first and
last name.")
e.Status = UpdateStatus.SkipCurrentRow
e.Row.RejectChanges()
End If
End If
End Sub

I hope this helps?

Cor
 
Hi Cor,
Thanks for the response.

I gave it a go, and this time no execution errors when I play around
with the form, but it's still not doing what it should. See, the book
example is supposed to show how to prevent users from leaving any of
two textbox fields blank before clicking update. I can still leave a
field blank ( eg LastName ) and it'll be updated into the database.
This must be a fairly standard check for textboxes on forms?

Your point about it being a crazy place to test a value - I think I
know what you mean ( I'm only learning ) but I'm beginning to wonder
about this book. Still, only on page 45 so time to improve..


Bob
 
Hi Bob,

This is so a crazy place to do this testing.
Think about this scenario (it is only one of them)
- record is readed with an empty textbox
- user has not changed
- message comes that the update will not be done
- but because that it was empty it stays empty
And there are hundreds of scenario's I think for this.

I do not say that this is the best method, but have a look by example at
this.

http://msdn.microsoft.com/library/d...emwindowsformscontrolclassvalidatingtopic.asp


Cor
 
Back
Top