.NET/SQL TimeOut Problem

I am calling a T-SQL stored procedure from VB.NET. It works fine when it is asked to return results for 30 students, but for larger numbers of students, it times out in 30 seconds. How can I best extend this TimeOut time?
Charles L BelnaOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi belna;

You can change the command timeout as shown below.

using (SqlConnection connection = new SqlConnection(connectionString)) {
    SqlCommand command = new SqlCommand(queryString, connection);
    // Setting command timeout to 1 minute / 60 seconds
    // The parameter is set in seconds
    command.CommandTimeout = 60;

    // Other code here

Open in new window

Ramkisan JagtapLead DeveloperCommented:
You can set it in connection string in your web.config as below :
<add name="ConnectionString" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;User ID=sa;Password=sa@123;Connect Timeout=200" providerName="System.Data.SqlClient"/>
Fernando SotoRetiredCommented:
Hi belna;

The two solutions posted so far are addressing two different problems. my solution is adjusting the CommandTimeout. In order to get this error your command had to have made it to the server without issue and is processing the command but the command has taken more then 30 seconds to process, which is the default for this command, and so it fails. The solution posted by Ramkisan Jagtap will occur if when a command was issued to the server but was unable to connect to the server with in 15 seconds which is the default unless you have adjusted it to some other value.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Maybe the issue is not the timeout; but the stored procedure.  30 seconds is a long time and I'd wonder if the SP is optimized correctly.  Just a thought.
Charles L BelnaOwnerAuthor Commented:
Thanks Fernando!
Fernando SotoRetiredCommented:
Not a problem Charles, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.