locked
Randomly Occuring -System.Data.SqlClient.SqlException login failed error RRS feed

  • Question

  • Dear All, 

    I need help on this,

    I have a project which is already hosted on the server. am getting SQLexception randomly. I tried every solution which is available on StackOverflow and asp.net forums. Please find my code which throwing exception

    My Controller

    [CustomHandleError]
    public class HomeController : Controller
    {
    Domain domain;
    public HomeController()
    {
    domain = new Domain();
    }
    
    public ActionResult Index()
    {
    HomePageViewModel vm = new HomePageViewModel();
    vm.Page = domain.GetHomePageInfo();
    vm.NewsLetters = domain.GetNewsLetters();
    
    if (vm.Page != null)
    {
    ViewBag.Title = vm.Page.PageTitle;
    ViewBag.Keywords = vm.Page.PageKeywords;
    ViewBag.Description = vm.Page.PageDesc;
    ViewBag.PageImage = vm.Page.PageTabImage;
    ViewBag.ColorCode = vm.LatestNewsLetter.IssueColorCode;
    
    return View(vm.Page.PageTemplate, vm);
    }
    return View(vm);
    }
    }
    
    public class Domain
    {
    #region Global Declarations
    Data dataAccess;
    SDataContext context;
    
    #endregion
    public Domain()
    {
    dataAccess = new Data();
    context = new SDataContext();
    
    
    }
    public Page GetHomePageInfo()
    {
    try
    {
    using (SDataContext sContext = new SDataContext())
    {
    Page pagedata = new Page();
    
    pagedata = sContext.Page.Where(x => x.PageIsHomePage == true && x.PageActive == true && x.PageVisible == true).FirstOrDefault();
    return pagedata;
    }
    }
    catch (Exception ex)
    {
    CustomFilters.logerrortextfile.errorlog(ex, "GetHomePageInfo");
    throw ex;
    }
    
    
    }
    }
    
    DBContext
    
    
    public class SDataContext: DbContext
    {
    public SDataContext()
    : base("name=SDataContext")
    {
    //Wthis.ContextOptions.LazyLoadingEnabled = false;
    Database.SetInitializer<SDataContext>(null);
    }
    
    
    
    public virtual DbSet<User> User { get; set; }
    public virtual DbSet<CMSUser> CMSUser { get; set; }
    public virtual DbSet<Page> Page { get; set; }
    public virtual DbSet<Country> Country { get; set; }
    public virtual DbSet<Language> Language { get; set; }
    public virtual DbSet<Error> Error { get; set; }
    public virtual DbSet<SNewsLetter> SNewsLetter { get; set; }
    
    
    public virtual DbSet<Album> Albums { get; set; }
    public virtual DbSet<AlbumImage> AlbumImages { get; set; }
    
    
    //Front end dbsets
    public virtual DbSet<Subscribe> Subscribe { get; set; }
    public virtual DbSet<LibraryImages> LibraryImages { get; set; }
    public virtual DbSet<ShareImages> ShareImages { get; set; }
    public virtual DbSet<ShareImagesCookies> ShareImagesCookies { get; set; }
    
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    base.OnModelCreating(modelBuilder);
    }
    
    
    
    }

    My Web.config

    <configuration>
    <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </configSections>
    
    <connectionStrings>
    
    
    <add name="SDataContext" connectionString="Data Source=sqlcluster1.apps.ae;Initial Catalog=sample_Mvc;User ID=Sadb;Password=s@mple123;Persist Security Info=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
    <compilation targetFramework="4.5" debug="true"/>
    <httpRuntime targetFramework="4.5" maxRequestLength="2147483647"/>
    <authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880"/>
    </authentication>
    <customErrors mode="On" defaultRedirect="~/Error/General"/>
    <!--<customErrors mode="RemoteOnly" ></customErrors>-->
    </system.web>
    <system.webServer>
    <security>
    <requestFiltering>
    <requestLimits maxAllowedContentLength="2147483647"/>
    </requestFiltering>
    </security>
    <!--<staticContent>
    <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
    </staticContent>-->
    </system.webServer>

    Exception 

    source :EntityFramework

    Method Name :GetHomePageInfo

    StackTrace : at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
    at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
    at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
    at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
    at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
    at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
    at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
    at System.Data.Entity.Internal.InternalContext.SaveChanges()
    at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
    at System.Data.Entity.DbContext.SaveChanges()
    at SWebsite.DomainLogic.Domain.LogError(Error error)
    at SWebsite.CustomFilters.CustomHandleError.OnException(ExceptionContext filterContext)
    at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
    at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
    at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
    at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
    at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
    at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
    at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
    at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
    at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

    Inner Exception: System.Data.SqlClient.SqlException (0x80131904): Cannot open database "dbname" requested by the login. The login failed.
    Login failed for user 'username'.
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
    at System.Data.SqlClient.SqlConnection.Open()
    at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
    at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
    at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
    at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
    at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
    ClientConnectionId:6b7b1cdb-0fd8-4215-bfa8-a76561e38e8d
    Error Number:4060,State:1,Class:11

    This exception only occuring after some idel time when any user request the site . Please anyone help me on this issue

    Thursday, January 11, 2018 2:02 PM

All replies