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