Solved

Time out in a SQL Query VB.NET

Posted on 2011-09-17
8
313 Views
Last Modified: 2012-05-12
Hi all,

Im getting the following message when updating records on a SQL Server:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

The code is very simple:

Dim cadena2 As String = "update available_mills set status=1 where id=99 and location=@f3"
Dim cmd2 As New SqlCommand(cadena2, con2)
cmd2.CommandType = CommandType.Text
cmd2.Parameters.AddWithValue("@f3", F3)
cmd2.ExecuteNonQuery()

This updates about 4000 records every hour

Thanks!
0
Comment
Question by:pvg1975
[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
8 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 36554544
set connection timeout to max

con2.timeout=0 this will be no time out
0
 

Author Comment

by:pvg1975
ID: 36554559
Im getting

Error      1      'timeout' is not a member of 'System.Data.SqlClient.SqlConnection'.

When using con2.timeout=0
0
 

Author Comment

by:pvg1975
ID: 36554589
I used this:

con2.CreateCommand.CommandTimeout = 0

And it seems to work. Am I doing it right? or createcommand is for something else?

Thanks!
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 15

Accepted Solution

by:
Haris Djulic earned 250 total points
ID: 36554755
it is beter to use the timeout with the command since:

SqlCommand.CommandTimeout = timeout limit for your SQL query. Means, how much time a (eg: SELECT, UPDATE) query can take for its execution. If it exceeds SqlCommand.CommandTimeout, then it stops execution. A command timeout error will occur.

SqlConnection.ConnectionTimeout = timeout limit for your connection. Means, how much time your connection object can try to connect. If it exceeds the specified time, it stops connecting. A connection timeout error will occur.

you can use it like that

cmd2.CommandTimeout =0
0
 

Author Comment

by:pvg1975
ID: 36554888
Thanks, i tried it but i get an error. I will paste the error once im back home. Thanks.
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36555928
A sqlConnection has a ConnectionTimeout property. It  is a property which determines the maximum number of seconds your code will wait for a connection to the database to open. It is set in the connectionstring to the database

data source=BROCHIS;initial catalog=Indato;integrated security=SSPI;Connect Timeout=60;

Opening the connection does not do anything in the database yet. That is handled by a SqlCommand, By default this is 30 seconds. When you perform something costly in the database this is the one whose value you’ll have to change.

SqlCommand cmd;

cmd.CommandTimeout = 60;

Hope this will work...
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 36556778
There is simply no reason to increase the CommandTimeout (let alone set it to 0-Infinte) for such a simple UPDATE.  All you need to do is optimize your query.

Do you have an index on id, location?
0
 

Author Closing Comment

by:pvg1975
ID: 36556815
That worked perfect. I also optimized the table and added an index to the ID field. Thanks guys!
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

Suggested Solutions

Title # Comments Views Activity
Set arraylist as datatable 2 29
I am new to using JSON in SQL Server 2 58
Run software updates from the website 6 50
Updating Printer Details in VB.Net 16 43
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

759 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