Solved

Timeout on SqlDataAdapter Fill to DataTable

Posted on 2014-03-16
3
2,024 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:BobCSD
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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 1

Author Comment

by:BobCSD
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
Yes that is fine as we'll.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now