Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

Timeout on SqlDataAdapter Fill to DataTable

I have a query that when run via sql server management studio, it takes 30 seconds. But when I run it within my source code to populate a DataTable, it errors with a timeout.

Here is my code that uses the query to populate the DataTable. It errors right after the Fill:

      Public Function GetDataTable(ByVal sql As String) As DataTable

         Dim con As New SqlConnection(Name.GetConnectionString)
         Dim adapter As New SqlDataAdapter()
         adapter.SelectCommand = New SqlCommand(sql, con)
         Dim myDataTable As New DataTable()
         con.Open()
         Try
            adapter.Fill(myDataTable)
         Finally
            adapter.Dispose()
            con.Close()
         End Try
         Return myDataTable

      End Function

Open in new window


Is there something wrong with my code above that would cause the same query that runs successfully in SSMS to fail with a web project? It goes straight to the Finally block.

thanks.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Starr Duskk

ASKER

I did this and it worked too. Is that 6 of one half a dozen of the other, or is the way you just showed me different?

thanks!

         Dim adapter As New SqlDataAdapter()
         adapter.SelectCommand = New SqlCommand(sql, con)
         adapter.SelectCommand.CommandTimeout = 300   ' 3/16/2014
         Dim myDataTable As New DataTable()

Open in new window

Yes that is fine as we'll.