Dataset Constraints - again

W

Woody Splawn

Last week sometime I posted the message below but I, unfortunately, have not
yet received an answer to the problem. The question has to do with how to
intercept a constraint viloation message in code and give the user a
different message. Someone responed by saying that I should use a try catch
statement, which is obvious, but the question is, where do I place it?
Where do I place such a try catch or some other code to catch a constraint
violation before VS shows the default message. The original question
follows:

A question please.

Lets say I have a grid with its datasource as a dataset with three columns
in it; UserID (an auto generated ID), LastName and FirstName. Additionally,
lets say I want to restrict the user from entering two records with the same
last name and first name. I have created a constraint for LastName
FirstName in the dataset but I would like a different message than the
default message I get when the constraint is violated. Is there a way for
me to do this?
 
A

AMDRIT

Have a look at:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemDataCommonDbDataAdapterClassFillErrorTopic.htm

Here is what I have been doing.

Public Function GetAddressesList() As DataDictionary

Dim ds As DataDictionary
ds = New DataDictionary

Try
daAddresses.Fill(ds.Addresses)
ds.Addresses.PrimaryKey = New DataColumn()
{ds.Addresses.Columns("AddressID")}
Catch ex As System.Data.ConstraintException 'Ignore this exception

Catch ex As Exception
Throw New ApplicationException("Unable to fetch ""Addresses""", ex)
End Try
Return ds

End Function


'Synchronizes local Address changes with the SQL server
Private Sub UpdateAddresses(ByVal AddressChanges As DataDictionary)


'Handle row inserts, sync identity updates
AddHandler AddressChanges.Addresses.ColumnChanging, New
DataColumnChangeEventHandler(AddressOf AddressRow_ColumnChanging)
AddHandler daAddresses.RowUpdated, AddressOf AddressRow_RowUpdated
AddHandler daAddresses.FillError, AddressOf Addresses_FillError
Dim iRet As Integer

'Update the local changes with the SQL database
iRet = daAddresses.Update(AddressChanges.Addresses)

'Remove handlers for row inserts and sync identity updates
RemoveHandler daAddresses.FillError, AddressOf Addresses_FillError
RemoveHandler daAddresses.RowUpdated, AddressOf AddressRow_RowUpdated
RemoveHandler AddressChanges.Addresses.ColumnChanging, AddressOf
AddressRow_ColumnChanging

End Sub
 
W

Woody Splawn

Thank you for responding.

I mean no offense to you or anyone else but man that is complicated for such
an easy thing to do in other tools. I hope others have an opinion or a
simpler way of doing this.

Basically, I want to catch the constraint violation as soon as the user
tries to leave the row of a grid, not just at the time he fills the dataset.

In the Infragistics Ultragrid there is an event that deals with this kind of
thing rather easily [UltraGrid_Error Event]. However, we would like to not
use third party tools unless absolutely necessary. Just thought there might
be a way to do this using native Visual Studio without making a federal case
out of it.

Again, the idea is to catch the constraint violation and deal with it when a
user leaves a row in a grid (or a row in a form for that matter), giving the
user a message other than the stock message provided by Visual Studio.
Example: A row with First Name, Last Name.

I sure hope VS 2005 deals with this and a number of other things like it in
a simpler way.

Woody
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top