ConnectionString属性尚未初始化IIS

时间:2016-10-20 16:11:18

标签: asp.net-mvc iis

将应用程序部署到IIS时遇到一些问题。这只发生在两个页面上,我的网站的其余部分在访问数据库时工作得很好。

  public static List<Parent> GetParentsWithAssignedStudentsByISDId()
    {

        SqlConnection sqlConn = null;
        SqlDataReader sqlDataReader = null;
        string sConnString = ConfigurationManager.AppSettings["SMARTDBConn"];
        var parents = new List<Parent>();

        try
        {
            sqlConn = new SqlConnection(sConnString);
            sqlConn.Open();
            string commandText = String.Format("SELECT * FROM dbo.ParentFullInfo WHERE ISDId = {0}", Utilities.getISDId());
            SqlCommand sqlCommand = new SqlCommand(commandText, sqlConn);
            sqlDataReader = sqlCommand.ExecuteReader();
            while (sqlDataReader.Read())
            {
                var parent = new Parent() { Students = new List<Student>() };
                parent.Id = Convert.ToInt32(sqlDataReader["ParentId"]);
                parent.ParentName = sqlDataReader["ParentName"].ToString();
                parent.EmailAddress = sqlDataReader["EmailAddress"].ToString();
                parent.Address = sqlDataReader["Address"].ToString();
                parent.City = sqlDataReader["City"].ToString();
                parent.State = sqlDataReader["State"].ToString();
                parent.ZipCode = sqlDataReader["ZipCode"].ToString();
                parent.WorkNumber = sqlDataReader["WorkNumber"] == DBNull.Value ? string.Empty : sqlDataReader["WorkNumber"].ToString();
                parent.PrimaryCellNumber = sqlDataReader["PrimaryCellNumber"] == DBNull.Value ? string.Empty : sqlDataReader["PrimaryCellNumber"].ToString();
                parent.HomeNumber = sqlDataReader["HomeNumber"] == DBNull.Value ? string.Empty : sqlDataReader["HomeNumber"].ToString();
                parent.Password = sqlDataReader["Password"].ToString();


                var student = StudentFactory.GetStudentByStudentId(Convert.ToInt32(sqlDataReader["StudentId"]));

                if (!parents.Exists(p => p.Id == parent.Id))
                {
                    parent.Students.Add(student);
                    parents.Add(parent);
                }
                else
                {
                    var par = parents.FirstOrDefault(p => p.Id == parent.Id);
                    if (par != null)
                        par.Students.Add(student);
                }


            }
        }
        finally
        {
            if (sqlDataReader != null) sqlDataReader.Close();
            if (sqlConn != null) sqlConn.Close();
        }

        return parents;
    }

我的Web.config确实有正确的密钥

    <add key="SMARTDBConn" value="server=ConnStringHere" />

错误

[InvalidOperationException: The ConnectionString property has not been initialized.]
   System.Data.SqlClient.SqlConnection.PermissionDemand() +6616256
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +6610951
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +233
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +278
   System.Data.SqlClient.SqlConnection.Open() +239
   SMART_Library.Factories.StudentFactory.GetStudentByStudentId(Int32 studentId) +138
   SMART_Library.Factories.ParentFactory.GetParentsWithAssignedStudentsByISDId() +1378
   SMARTSupport.Controllers.ParentController.Search(String SearchKey) +48
   lambda_method(Closure , ControllerBase , Object[] ) +127
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +270
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +120
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +74
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

0 个答案:

没有答案