Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch exceptions in databound forms 1

Status
Not open for further replies.

fawkes

Technical User
Sep 12, 2003
343
GB
I have a collection of objects and a detail form where the controls of the form are databound to one of the objects in the collection.

When the name property of the object is changed a handler in the collection checks to ensure that the name is unique, if not it throws a system.argumentexception.

How can I catch the exception when the control on the form tries to validate the entry for the name property?
 
Are you asking how to catch an exception, or which event to handle in order to catch the exception?
 
Either will do, which is the best practice?
 
If you already know where the exception is being thrown you can try to catch it using a Try...Catch...End Try block

e.g.
Code:
		Try
			'do something here that might not work
		Catch ex As Exception
			'handle known problems and clean up
			'pass on the error information to calling method
			Throw ex
			'OR - pass on something more meaningful
			Throw New Exception("Detailed Message" & vbCrLf & ex.Message)
			'OR - pass info direct to user
			MsgBox(ex.Message)
			Exit Sub
		Finally
			'Something you always want to happen whether there's an exception or not
		End Try
In your scenario there isn't a calling method as your exception is happening within an event handler, so you can only pass on the error info (or a more helpful message) and tidy up.

You may want to use the Finally clause for code that should be executed whether or not there is an exception. In this case, if an exception is thrown the finally clause is executed after anything in the catch clause and before the exit statement.

Hope this helps. There are some notes on Try Catch in Visual Studio help, but a lot of stuff like Finally and catching different exception types is not in the main help section for this.
 
The probelm is that the exception is being through when the text in a text box is edited and I can't see how to catch this expection.

I'll add some more meat to the bones.

I have a form with a datagridview whose datasource is a collection of obects. When I want to edit the collection of objects a new form is opened and the object is passed to the new form and bound to the gui controls (text boxes, etc.).

When one property of the object is changed an event is raised and the collection handles this event, performs some validation code and throws an exception if the new value for the property isn't valid.

It is this exception I can't catch.
 
You can catch it in the parse. when you set up your binding:

Code:
    ''' <summary>
    ''' Sets up a data binding between specified column and specified control
    ''' </summary>
    ''' <param name="Column"></param>
    ''' <param name="Ctrl"></param>
    ''' <remarks></remarks>
    <Description("Sets up a data binding between specified column and specified control")> _
    Private Sub CreateBinding_TextBox(ByVal Column As String, ByVal dt As DataTable, ByVal Ctrl As TextBox)

        Dim dbnIco As Binding = New Binding("Text", dt, Column)
        AddHandler dbnIco.Format, AddressOf NullToString
        Ctrl.DataBindings.Add(dbnIco)

    End Sub


Then, add the sub:
Code:
 ''' <summary>
    ''' Handles the Format event for the Region TextBox.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    <Description("Handles the Format event for the Region TextBox.")> _
    Protected Sub NullToString(ByVal sender As Object, ByVal e As ConvertEventArgs)

        If IsDBNull(e.Value) Or e.Value.ToString.Trim.Length = 0 Then
            e.Value = ""
        End If

    End Sub

    ''' <summary>
    ''' Handles the Parse event for the Region TextBox.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    <Description("Handles the Parse event for the Region TextBox.")> _
    Protected Sub StringToNull(ByVal sender As Object, ByVal e As ConvertEventArgs)
        If e.Value.ToString = "[N/A]" Or e.Value.ToString.Trim.Length = 0 Then
            Try
                e.Value = DBNull.Value
            Catch exp As Exception
                MessageBox.Show("Data entry error: " & exp.Message, Me.Text, _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub




 
Thanks for the tip, star for you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top