Where to put code...

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
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:

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:
You should catch the exception using a Try...Catch block and then set a breakpoint in the Catch block. When the debugger halts at that point you can examine the exception object and determine its exact type.
 
OK Where should I put the Try..Catch block? I already have two that the wizard created:

Public Sub UpdateDataSet:

VB.NET:
[COLOR=#0000ff]Try[/COLOR]
[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]

and Private Sub UpdateDataSource:

VB.NET:
[COLOR=#0000ff]Try[/COLOR]
[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]

I have tried to add error code where it says 'Add your error handling code here.
That was a simple messagebox of show eUpdate.message, and show updateException.message from the Catches.
This didn't do much....

Sorry if I sound a bit dim here, I haven't done any programming on the systems for a while due to a promotion, so am like a newbie all over again!!!

Regards,
 
Hmm may of been a bit hasty!! lol

I "assumed" that if an exception is caught, it halts on the rest of the sub, obviously I've incorrectly assumed that.

Here's what I am trying to do:

VB.NET:
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].UpdateDataSet_Computers()[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] UpdateEx [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MessageBox.Show(UpdateEx.Message)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
 
[SIZE=2]MessageBox.Show("Computer successfully added to the audit!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TabControl1.SelectedIndex = 1[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtSoftwareNetBIOS.Text = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtNetBIOS.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].rbLaptop.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].gbLaptop.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].gbLaptop.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cboOperatingSystem.Focus()[/SIZE]

So if an exception is caught, and a messagebox is displayed, it still goes ahead and does the rest of the sub (from the computer successfully added messagebox onwards).

Is there a way so that if the error is caught and the messagebox shown to the user that the ComputerName is already in the audit, it halts? otherwise if there is no error then it should carry on, display that it has been added and continue with the sub???

Thanks!
 
The point of a Catch block is that you do what needs to be done to clean up when an error occurs without crashing the app, i.e. it continues normally after that point. If what needs to be done is that you exit the method then it's up to you to do that. How do you normally exit a method before the end?
 
Back
Top