Solved

Timeout on SqlDataAdapter Fill to DataTable

Posted on 2014-03-16
3
2,450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 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 63

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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