Visual Studio .NET Code Snippets:
Debug Binding Errrors

 
'' this code will try binding the form, removing one binding at a time until it succeeds 
'' if you look at your debugger output, it will print out each control it is removing the 
'' binding from until it succeeds. The last control printed will have a binding error. 
'' 
'' Lots of room for cleaning up this code yet.. 
 
 
 
'' this section goes in your main class to setup exception handlers 
Public Sub Main() 
	Dim currentDomain As AppDomain = AppDomain.CurrentDomain 
 
	'' trap unhandled exceptions and thread exceptions 
	AddHandler currentDomain.UnhandledException, AddressOf HandleUnhandledException 
	AddHandler Application.ThreadException, AddressOf HandleThreadException 
	 
	dim frm as new myform 
	Application.Run(frm) 
end sub 
 
Public ignorethreadexception As Boolean = False 
Public threadexception As Boolean = False 
'Handles the exception event 
Public Sub HandleThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs) 
	threadexception = True 
	If Not ignorethreadexception Then '' handle thread exception 
	end if 
End Sub 
 
 
'' put this section in your form where you want to test binding 
#If DEBUG Then 
				Dim boundok As Boolean = False 
				ignorethreadexception = True 
				While (Not boundok) AndAlso ClearFirstBinding() 
					Try 
						threadexception = False 
						'' sample code to cause binding error 
						'' could also suspend databinding then restore it 
						'' (cm is currency manager for dataset) 
						tabcontrol.visible = true 
						cm.Refresh() 
						'' end code to cause binding error 
						If Not threadexception Then boundok = True 
						threadexception = False 
						If boundok Then Trace.WriteLine("BOUND OK!") 
					Catch ex As Exception 
 
					End Try 
				End While 
	threadexception = False 
	ignorethreadexception = False
 							 
#End If 
 
'' put these routines whereever you want to call them: 
 
    ''' ----------------------------------------------------------------------------- 
    ''' <summary> 
    ''' Clear bindings 
    '''   one   by one to see which one breaks 
    ''' </summary> 
    ''' <remarks> 
    ''' </remarks> 
    ''' <history> 
    ''' 	[greg.pringle]	28/06/2005	Created 
    ''' </history> 
    ''' ----------------------------------------------------------------------------- 
    Protected Function ClearFirstBinding() As Boolean 
        For Each c As Control In Me.Controls 
            If c.DataBindings.Count > 0 Then 
                Trace.WriteLine("Clearing bindings for " + c.Name) 
                c.DataBindings.Clear() 
                Return True 
            End If 
            If ClearFirstBinding(c) Then Return True 
        Next 
        Return False 
    End Function 

    Protected Function ClearFirstBinding(ByVal co As Control) As Boolean 
        For Each c As Control In co.Controls 
            If c.DataBindings.Count > 0 Then 
                Trace.WriteLine("Clearing bindings for " + c.Name) 
                c.DataBindings.Clear() 
                Return True 
            End If 
            If ClearFirstBinding(c) Then Return True 
        Next 
        Return False 
    End Function