0

I need to change the timeout value for all TableAdapters on a form. I have a function that works for a single component but does not work when I try and pass in values from a list of components I created from the form. I get "Object reference not set to an instance of an object." for the items in the list. the error is from the TableAdapterCommandTimeout function, this works when i pass in normal componet on the form (TableAdapter1 thats on the form designer). Any help would be greatly appreciated, Thanks.


' main function code
Public Sub GetComponents(ByVal frm As Form)        
Dim comsList = GetTableAdapterList(frm)
        For Each item As Component In comsList
            TableAdapterCommandTimeout(item, 300)
        Next
End Sub
 

' code to get list of Table Adapters
Public Function GetTableAdapterList(ByVal frm As Form) As List(Of Component)
Dim components As New List(Of Component)
Dim fieldInfos() As FieldInfo = frm.GetType.GetFields(BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
        For Each f As FieldInfo In fieldInfos
            If f.FieldType.BaseType Is GetType(Component) Then
                Dim c As Component = TryCast(f.GetValue(frm), Component)
                If c IsNot Nothing And c.ToString.Contains("TableAdapter") AndAlso Not c.ToString.Contains("TableAdapterManager") Then components.Add(c)
            End If
        Next
        Return components
    End Function
 

' code to update timeout
Public Shared Sub TableAdapterCommandTimeout(Of T As Global.System.ComponentModel.Component)(ByRef TableAdapter As T, CommandTimeout As Integer)
        If (TableAdapter IsNot Nothing) Then
            Dim pi = GetType(T).GetProperty("CommandCollection", BindingFlags.NonPublic Or BindingFlags.GetProperty Or BindingFlags.Instance).GetValue(TableAdapter, Nothing)
            If pi IsNot Nothing Then
                For Each c In TryCast(pi, System.Data.SqlClient.SqlCommand())
                    If (c IsNot Nothing) Then
                        c.CommandTimeout = CommandTimeout
                    End If
                Next
            End If
        End If
    End Sub
 
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Gilad Green Oct 03 '16 at 22:05
  • Have changed it to .net. The problem is not how to deal with a NullReferenceException, it's why is it getting it as it is passing an object in but there must be a difference between passing a standard parameter (listed on the form designer) and an item from the list of objects. I don’t know what that is so I am asking for help. – Zim Destroy Oct 03 '16 at 22:14

0 Answers0