0

I am using asp.net(c#) with sql server connection. This application is used by 30 users at a time.

Below is the connection string of my application :

<add name="AOS" connectionString="Data Source=SANDIP-PC\R2;Initial Catalog=DB_20150322;User ID=sa;Password = lambda; Connect Timeout=100; Pooling=False;" providerName="System.Data.SqlClient"/>

My application is running fine with the above connection string,but sometimes when exception occurs and at that time if connection was not closed and during that time any new request comes then it displays the following error message :

"Cannot open database "DB_20150322" requested by the login. The login failed.

Login failed for user 'sa SqlException (0x80131904)'."

I don't have source code of this application. How can I resolve this? How to close database connection globally when such exception occurs? If there any other issue other than my thinking process?

I am in this thought because same page opens every time without any error , but sometimes when the same page is loaded with the same criteria for the first time , the page doesn't load.

How to close connection globally when such exception occurs?

Do I need to change anything in connection string?

Community
  • 1
  • 1
Sandip
  • 49
  • 1
  • 1
  • 8

1 Answers1

0

It's not the Connection Close issue, It's showing Login failed Error because of Authentication Mode used in Web.Config file.

use Windows Authentication to impersonate the service account, you have to set up Windows Authentication in both IIS and ASP.NET.

Set in Web.Config File:

<system.web>
    <authentication mode="Windows"/>
 </system.web>

EDIT2: if it really happening because of the connection issue then you can check the connection Status when opening or closing any connection, like

if(con.State == ConnectionState.Open)
{
con.Close();
}

and

if(con.State == ConnectionState.Close)
{
con.Open();
}
A_Sk
  • 4,532
  • 3
  • 27
  • 51
  • Sorrry,I am not sure here what you mean to say.It already in web.config authentication mode="windows". can you elaborate more on this? – Sandip Mar 29 '15 at 06:59
  • the error `The login failed. Login failed for user 'sa SqlException (0x80131904`,see http://stackoverflow.com/questions/22415319/system-data-sqlclient-sqlexception-login-failed-for-user – A_Sk Mar 29 '15 at 07:01