Visual Studio .NET - sql connection problem - Asked By shekhar kumar on 03-Dec-08 06:58 AM

I am writing my connection string in the app code folder as follows

private static SqlConnection connection;
 public static SqlConnection GetOpenConnection()
    {
        if (connection == null)
        {
            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HandicapIndia"].ConnectionString);
            connection.Open();
            return connection;
        }
        else
            return connection;
    }


It's is working fine. But after some hours it get an error on the page where i am using it as follows

SqlCommand com = new SqlCommand("commandText, db.GetOpenConnection());

Please help me.

Thanks

clarify this - ram kumar replied to shekhar kumar on 03-Dec-08 07:07 AM

Hi,

any specific error?

may i know what is wrong with the code. - Asked By shekhar kumar on 03-Dec-08 07:23 AM

Thanks for your response.

But why after some hours it is giving error.
Is it right to return open connection from the app code folder.

Thanks Again.

what error you are getting? - Eswaran Radhakrishnan replied to shekhar kumar on 03-Dec-08 07:31 AM

Hi,

what error you are getting ?

Thanks

RE

Error - shekhar kumar replied to Eswaran Radhakrishnan on 03-Dec-08 07:34 AM
Thanks for response.

I am using executscalor in one of my aspx pages.

There I am getting error Required open connection.

Thanks once again.


re - Web Star replied to shekhar kumar on 04-Dec-08 01:00 AM

due to session timeout,

u should check for that.

Thanks to all of you. - shekhar kumar replied to Web Star on 09-Dec-08 06:38 AM
I got the solution. Basically I was not checking the state of the connection.

The right code is what I have written and is working for me is

private static SqlConnection connection;
    public static SqlConnection GetOpenConnection()
    {
        if (connection == null || connection.State==ConnectionState.Closed || connection.ConnectionString!="")
        {
            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HandicapIndia"].ConnectionString);
            connection.Open();
            return connection;
        }
        else
            return connection;
    }

Thanks Once Again