0

I get this error:

The wait operation timed out 

And it occurs on this line:

da.Fill(result);

On the following code:

public static DataTable Get_Table_Grouped()
{
    DataTable result = new DataTable();

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["My_Data"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(@"
        SELECT TOP 10  Attribute1, Attribute2, Attribute3, Attribute4, Attribute5, 
        Sum([Attribute6]) As Attribute6
        FROM My_Table
        group by Attribute1, Attribute2, Attribute3, Attribute4, Attribute5", con))
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(result);
            con.Close();
            da.Dispose();
        }
    }

    return result;
}

The following is my connection string for "My_Data"

<add name="My_Data" connectionString="Data Source=Database_Location;User ID=username;Password=my_password;" providerName="System.Data.SqlClient"/>

What can I do to fix this? Is there a way to stop the timeout? When I try the same query directly in Microsoft SQL Server it works but after a while, I don't see anything in my code that causes the timeout

0

2 Answers 2

1

A timeout error means that the query took longer than the maximum time you defined. You need to improve the query, change the timeout value or check if there any locks on the table.

You can change the timeout value by doing this:

cmd.CommandTimeout = 600;

You can also specify the timeout value in the connection string by adding Connection Timeout=600 at the end. In your case:

Data Source=Database_Location;User ID=username;Password=my_password;Connection Timeout=600;
Sign up to request clarification or add additional context in comments.

Comments

0

The default value for a command timeout is 30 seconds. If your query is pulling in a massive amount of data, or is slow, you'll have to manually set the timeout value. Francisco offered a couple possibilities, but another way you can increase the timeout is through your SqlDataAdapter instance:

da.SelectCommand.CommandTimeout = MyIntValue;

Also, since you're calling your SqlConnection with a using statement, your con.Close() is actually redundant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.