Solved

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

Posted on 2013-11-21
6
515 Views
Last Modified: 2013-11-27
I have been having an issue connecting to a SQL database that I can’t seem to figure out.
I have created a very simple static function that checks a SQL database.  The database contains the NSRL (National Software Reference Library).  This is used to keep track of known system files.  It is useful when you are searching for user created content and you want to exclude known system files.

Originally I created this as a static class.  I then tested it using a console application.  Everything worked well.  I was able to scan every file on my computer and list which ones where known system files.

Now I am trying to implement this into my Windows Form application and I continue to get the error message below:

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

The error occurs on the following line:

command.ExecuteNonQuery();


I have checked my SQL connection string, at it is the same as in my test application.  My Windows Form and Test Console Application are also both running on the same machine.
I also went back and made this a non-static class and I get the same error message.  Below I have the code for my class.  I can’t seem to find the difference between how my Windows Form call this class as opposed to my test console application.

I have written all of this in C# 4.5 and SQL 2008 RC2

Any help would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;


namespace helper
{
    public class NSRLClass
    {

        public SqlConnection globalSQLConnection;

        public void startSqlConnection(string sqlConnectionString)
        {
            if (globalSQLConnection == null)
            {
                globalSQLConnection = new SqlConnection(sqlConnectionString);
                globalSQLConnection.Open();
            }

            if (globalSQLConnection == null)
            {
                globalSQLConnection = new SqlConnection(sqlConnectionString);
                globalSQLConnection.Open();
            }

        }

        public void closeSqlConnection()
        {
            if (globalSQLConnection != null)
            {
                globalSQLConnection.Close();
            }
        }

        public bool isInNSRL(string sqlConnectionString, string md5String, string fileSizeString, ref string OpSystemCodeString)
        {

            bool returnBool = false;

            if (globalSQLConnection == null)
            {
                globalSQLConnection = new SqlConnection(sqlConnectionString);
                globalSQLConnection.Open();
            }

            SqlConnection connection = new SqlConnection(sqlConnectionString);

            SqlCommand command = new SqlCommand("dbo.[pr_checkMd5AndSize]", globalSQLConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@md5", md5String);
            command.Parameters.AddWithValue("@filesize", fileSizeString);

            SqlParameter nameSqlParameter = new SqlParameter("@isInNsrl", SqlDbType.Int);
            nameSqlParameter.Direction = ParameterDirection.Output;
            command.Parameters.Add(nameSqlParameter);

            SqlParameter nameSqlParameter2 = new SqlParameter("@OpSystemCode", SqlDbType.VarChar, 50);
            nameSqlParameter2.Direction = ParameterDirection.Output;
            command.Parameters.Add(nameSqlParameter2);

            command.ExecuteNonQuery();

            OpSystemCodeString = nameSqlParameter2.Value.ToString();

            if (Convert.ToInt32(nameSqlParameter.Value.ToString()) == 0)
            {
                returnBool = false;
            }
            else
            {
                returnBool = true;
            }

            return returnBool;

        }

    }
}

Open in new window

0
Comment
Question by:rye004
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39667115
If you execute that procedure directly in SQL server does it return results in timely manner?
0
 

Author Comment

by:rye004
ID: 39667132
Yes it does.  SQL Server Management indicates it takes 0 seconds.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39667147
Then the problem is in your C# code in teh way you execute the sp. If you will execute it as an SQL statement with:

sql+query="EXEC dbo.[pr_checkMd5AndSize] @md5='"+md5String+"', @filesize='"+fileSizeString+"'"

and then execute that sql_query

should work.

I am not sure why you add 2 more parameters to that procedure in your code.

As a note I am not very familiar with C# and that way to execute procedures.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:rye004
ID: 39667610
Thank you for getting back to me.  I have tried what you have stated, and it still does not work.  This just really has me stumped why a static console application will work and a windows form will not.

In regards to the other parameters:

@isInNsrl is used to indicate if the file is a known file type.  I realized now that I could have just used the default return property.

@OpSystemCode is used to tell you the type of file, if it is a known file being returned.
0
 
LVL 1

Accepted Solution

by:
psreloaded earned 250 total points
ID: 39668177
Hi, you seem to have created two connection object and are using only one. Secondly, you seem to have opened the globalSQLConnection but never closed it. It should be avoided. Use the using construct instead which takes care of disposing the connection. I am not too sure whether thats the cause of this issue. Other than that the code looks pretty simple.

Did you run SQL profiler to check if the procedure is getting executed at all when called from the code?
0
 

Author Comment

by:rye004
ID: 39681951
I determined that this was due to my SQL database.  I created an index and worked performance, that seemed to do it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now