The wait operation timed out in asp.net mvc 4 - c#

I have a table contains 100 columns and 2,000,000 records.
When I am fetching records using stored procedure from that sometimes I am getting "The wait operation timed out." error.When I alter the stored procedure and try to fetch the records, it works fine.
Can anyone let me know what is the best solution for this ?

As vishal Naik has mentioned, SQL server has a default query timeout setting of 30 seconds. A possible solution would be to manually increase this time for a given query and this, while not recommended, should be effective. The code is as follows:
SqlCommand cmd = new SqlCommand(commandText, conn);
cmd.CommandTimeout = 60; // or any other length of time in seconds
/*Any other properties to be modified in the command will come here*/
SqlDataReader dataReader = cmd.ExecuteReader();

SQL server has default query timeout setting of 30 seconds.
For more details click here

Related

Why is timeout occurring while executing a stored procedure in C# even after increasing the commandtimeout property?

I have a piece of code that executes stored procedure.i have given a "Waitfor Delay '00:05' " in the SP for the purpose of testing the timeout in my C# code wherein my commandtimeout is set for 10 mins. While debugging, after 20 seconds or so when the executereader for the SP is executed,I am getting a timeout .
Stuck on this for 2 days, any help is much appreciated.
Give ConnectionTimeout as 0, it means undefined waiting time.And one more thing , u can debug the SP using EXEC command in sql server itself and check where the time taking in code or in SP.
Connection Timout in code
SqlDataAdapter da = new SqlDataAdapter(Query, ConnectionString);
da.SelectCommand.CommandTimeout = 0;
Execute SP in sql server
EXEC procedure_name;
Regards
Aravind

Getting SQL Server timeout exception even with command timeout set to 0

I have set the command timeout to 0 as per the documentation in SQL Server. I'm indexing a large table, and still get an exception "Execution timeout expired". The timeout period elapsed prior to completion of the operation or the server is not responding. The server is responding as I watch it though the SQL Server Monitor.
Here is the pertinent code:
private void ExecuteQuery(string qStr)
{
using (SqlConnection cnx = new SqlConnection(_ConnectionString))
{
cnx.Open();
using (SqlCommand cmd = new SqlCommand(qStr, cnx))
{
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
}
}
}
This is the connection string
Data Source='tcp:aplace.database.windows.net,1433';Initial Catalog='SQL-Dev';User Id='user#place';Password='password';Connection Timeout=360
Why am I getting a execution timeout? I have the connection timeout set to 7200 seconds, also. I am indexing a 31 million rows table on one column.
First, connection timeout and command timeout are not the same thing. Make sure you understand the difference and are using them correctly.
Second, if this is from a web page, you also need to consider timeout values relating to the web server, etc.
Third, to verify that it is in fact a timeout issue, execute your index statement in SSMS and find out how long it takes. Since the actual indexing takes place on the SQL server no matter where it is called from, the indexing time should be roughly equal whether running from SSMS or your application.

How can solve connection time out error in c#?

I'm new to C# and SQL Server; I wrote a simple stored procedure in T-SQL, and in C# with under code call that:
da = new SqlDataAdapter("select_equals_Cycle", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
ds = new DataSet();
da.Fill(ds, "select_equals_Cycle");
but on this line:
da.Fill(ds, "select_equals_Cycle");
I get this error:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
My connection string this:
string conn = "Data Source=.;Initial Catalog=ClubEatc;Integrated Security=True;Connect Timeout=30;";
How can I solve that? thanks.
Use CommandTimeout or optimize the StoredProcedure
da.SelectCommand.CommandTimeout = 180; // default is 30 seconds
Don't set the timeout value, if you don't know how to configure it.
Microsoft make the optional has well defined value.
the Timeout question has two items you have to check out.
Database:
e.g. MSSQL
--the code below ran in Query of Sql Server
--this code snippet will show you all the advance options
Exec sp_configure 'show advanced options',1
recogfigure
-- this code snippet configure the seconds of the query wait
-- this configuration means that if the memory couldn't used to query -- the big query, sql server will wait for some seconds
-- this option usually use the default value made by Microsoft
-- the default value about how many seconds of formula is the estimated query -- -- time multiply by 25, and if the memory still couldn't used for querying.
-- then throw TimeOut Exception
Exec sp_configure 'query wait', 200;
ReCONFIGURE
-- the end
// the timeout configuration about C#
SqlConnection.ConnectionTimeout = 200
// here is the Document:
// get the time to wait while trying to establish a connection before
// terminating the attempt and generating and error
// the Unit of the property is second
// 0 is no limit
after you scan the code snippet, you will find two reason can cause the exception.
your code couldn't establish the connection.
your memory in the machine install sql server couldn't used to run the big query

How to increase the connect timeout on a SqlCommand?

I have around 7000 rows in excel and I want send them to another local computer with linked server. I increased connect timeout in my connections but I get the same time again with no result.
SqlCommand cmd9 = new SqlCommand("insert into [" + ConString + "].Database.dbo.ExpressE select * from ExpressE", conn);
cmd9.ExecuteNonQuery();
Error message is:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated.
The SqlCommand has a property CommandTimeout that by default is set to 30 seconds. You can set it to a higher number of seconds, even set it to 0 if you want the command to wait forever if needed. The link to the MSDN includes an example about how to use that property.

using multiple transactions in single connection

Basically it's a patching mechanism
Here is what I'm doing :
Open a SQL connection.
Begin the transaction.
Update a record in database for the version of the software.
Execute some more queries on same database by using same connection.
Download a 15 to 20 MB file.
Execute a select query by using the same connection.
Commit the transaction.
Close the transaction.
This sequence is causing the problem of SQL Connection timeout as it takes time to download the file.
The problem is that I can commit the transaction only after downloading the file and not before that.
Writting the code in C#. Database used is SQLCE
Here is some part of the code:
SqlCeConnection conn = new SqlCeConnection("ConnectionString");
conn.Open();
SqlCeTransaction ts = conn.BeginTransaction();
//A method call executes all the methods that with parameters
(string sqlQuery, ref SqlCeConnection conn, SqlCeTransaction ts)
{
SqlCeCommand cmd = new SqlCeCommand();
cmd.Connection = conn;
cmd.Transaction = ts;
cmd.CommandText = sqlQuery;
cmd.ExecuteNonQuery();
}
//A method call downloads the file of 15 to 20 MB
//A method executes a select query that returns the version of the software by using same SQL connection.
//The above query gives the error of SQl connection timeout
ts.Commit();
conn.Close();
Can any one help me to solve the problem
Set you command timeout manually.
cmd.CommandTimeout = 180;
This example sets the Timeout to 180 seconds (3 minutes)
This is what I did.
I passed the same connection and transaction object to the method which was downloading the file.
In that method I executed a simple select query in the loop where the file was getting downloaded. This helped to keep the connection and transaction active.
Here even if your internet connection speed is slow it does not affect as in each loop your SQl query gets fired and keep the connection active.

Categories

Resources