The issue: Connection variable in a module. I overlooked this.
Dim MyConnection As New SqlClient.SqlConnection( _
ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
Answer: Remove the module level variable and add it as a parameter to your function
Public Function GetData2(ByVal Sql As String, _
MyConnection As SqlClient.SqlConnection) As DataTable
'...code...'
End Function
Note: As a better alternative to using public functions in a module, I've created a PageBase class and inherited it on the pages requiring the functions. I added the connection variable at the top of class as it will expose it only to the page inheriting it. Or... you can create the connection in the page code behind and pass it to the function.
Page Code Behind
Public Class MyPage
Inherits PageBase
End Class
PageBase Class
Public Class PageBase
Inherits System.Web.UI.Page
Public MyConnection As New SqlClient.SqlConnection( _
ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
Public Function GetData2(ByVal Sql As String) As DataTable
'...code...'
End Function
End Class
Conclusion: I tried both of these methods and they fixed the posted issue.