Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Timeout on SqlDataAdapter Fill to DataTable

Posted on 2014-03-16
3
Medium Priority
?
2,788 Views
Last Modified: 2014-03-16
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.
0
Comment
Question by:Starr Duskk
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39933019
Hi BobCSD;

Try increasing the CommandTimeout property of the SqlCommand object to see if that helps. Modify your code as shown below.

Dim adapter As New SqlDataAdapter()
Dim cmd As New SqlCommand(sql, con)
' Increase the timeout to a value greater then 30 which is the default. 
' This value is in seconds
cmd.CommandTimeout = 45
adapter.SelectCommand = cmd
Dim myDataTable As New DataTable()

Open in new window

0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39933061
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

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39933117
Yes that is fine as we'll.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question