Arg81
Well-known member
I've created a computer audit, and so computer names must be unique. If I try adding a new record with the same computer name, the constraint works because I get the default message of:
Column "NetBIOS" is constrained to be unique.
Value "Computer001" already exists
Continue or Quit
^^ I want to replace that standard error box with a messagebox showing the same information, so that the user has to change the computer name before continuing.
However, where do I put this code, and what is the error I need to trap? I'm using the code from the Data Form Wizard, so my update statments look like this:
Column "NetBIOS" is constrained to be unique.
Value "Computer001" already exists
Continue or Quit
^^ I want to replace that standard error box with a messagebox showing the same information, so that the user has to change the computer name before continuing.
However, where do I put this code, and what is the error I need to trap? I'm using the code from the Data Form Wizard, so my update statments look like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] UpdateDataSet_Computers()[/SIZE]
[SIZE=2][COLOR=#008000]'Create a new dataset to hold the changes that have been made to the main dataset.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objDataSetChanges [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Audit_Computers.dsAudit_UK = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Audit_Computers.dsAudit_UK[/SIZE]
[SIZE=2][COLOR=#008000]'Stop any current edits.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingContext(DsAudit_UK1, "Computers").EndCurrentEdit()[/SIZE]
[SIZE=2][COLOR=#008000]'Get the changes that have been made to the main dataset.[/COLOR][/SIZE]
[SIZE=2]objDataSetChanges = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](DsAudit_UK1.GetChanges, Audit_Computers.dsAudit_UK)[/SIZE]
[SIZE=2][COLOR=#008000]'Check to see if any changes have been made.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] (objDataSetChanges) [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'There are changes that need to be made, so attempt to update the datasource by[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'calling the update method and passing the dataset and any parameters.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].UpdateDataSource_Computers(objDataSetChanges)[/SIZE]
[SIZE=2]DsAudit_UK1.Merge(objDataSetChanges)[/SIZE]
[SIZE=2]DsAudit_UK1.AcceptChanges()[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] eUpdate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Exception[/SIZE]
[SIZE=2][COLOR=#008000]'Add your error handling code here.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Throw[/COLOR][/SIZE][SIZE=2] eUpdate[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Add your code to check the returned dataset for any errors that may have been[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'pushed into the row object's error.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] UpdateDataSource_Computers([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] ChangedRows [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Audit_Computers.dsAudit_UK)[/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'The data source only needs to be updated if there are changes pending.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] (ChangedRows) [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Open the connection.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].conAudit.Open()[/SIZE]
[SIZE=2][COLOR=#008000]'Attempt to update the data source.[/COLOR][/SIZE]
[SIZE=2]daComputers.Update(ChangedRows)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] updateException [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Exception[/SIZE]
[SIZE=2][COLOR=#008000]'Add your error handling code here.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Throw[/COLOR][/SIZE][SIZE=2] updateException[/SIZE]
[SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Close the connection whether or not the exception was thrown.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].conAudit.Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Last edited: