2016-10-16 57 views
0

我已經搜索的連接字符串屬性尚未初始化錯誤連接字符串屬性未初始化

谷歌以及堆棧溢出,但無法找到解決方案。我創建了一個與數據庫交互的數據庫類,所有相關的代碼都寫在這個文件中。問題是相同的代碼在其他頁面上運行良好,它只是不能在名爲「addevent.aspx」的頁面上工作。我不明白爲什麼它不能正常運行。

下面是我在database.cs創建的方法文件

public void CreateConnection() 
{ 
    var ConfiguredString = ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString; 
    obj_sqlconnection = new SqlConnection(ConfiguredString); 

} 

//This property will set the connection string for database 
public string ConnectionString 
{ 
    get 
    { //if _connectionString is already created or set, only then it will return the value of _connectionString 
     if (_connectionString != string.Empty && _connectionString != "" && _connectionString != null) 
      return _connectionString; 
     else 
      return string.Empty; 
    } 
    // When you want to set the connection string set block is called. 
    set 
    { // this line sets the connection string to the _connectionString data member for the first time. 
     if (_connectionString == string.Empty || _connectionString == "" || _connectionString == null) 
      _connectionString = value; 
    } 
} 

// Open database connection.  
public void OpenConnection() 
{ 
    obj_sqlconnection.Open(); 
} 

// Close database connection. 
public void CloseConnection() 
{ 
    obj_sqlconnection.Close(); 
    obj_sqlconnection.Dispose(); 
} 

public SqlConnection GetCurrentConnection 
{ 
    get { return obj_sqlconnection; } 
    set { obj_sqlconnection = value; } 
} 

我根本就不懂這個錯誤,其發生的邏輯。我得到這個錯誤,當我打開連接

如何調用這些方法,我已經創建database.cs類的對象與對象名稱MYDB方法的addEvent

public int AddEvent(string _title, string _description, string _place, int _eventTypeID, string _startingTime, string _endingTime, string _startingDate, string _endingDate, string _creatorID, string _picture) 
{ 
    string[] blacklist = { _title, _description, _place, _picture }; 

    if (Jvalidate.FilterBlackLIstKeywords(blacklist)) 
    { 
     int eventid = Convert.ToInt32(mydb.GetLastValueByColumnName("event_id", "tbl_events")); 
     int rowsaffected = 0; 
     mydb.CreateConnection(); 

     mydb.InitializeSQLCommandObject(mydb.GetCurrentConnection, "spAddEvent", true); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@eventID", eventid + 1); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@title", _title); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@description", _description); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@place", _place); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@eventType", _eventTypeID); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@startingTime", _startingTime); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@endingTime", _endingTime); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@startDate", _startingDate); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@endDate", _endingDate); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@schoolID", SchoolID); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@eventCreatorID", _creatorID); 
     mydb.obj_sqlcommand.Parameters.AddWithValue("@eventPicture", _picture); 
     try 
     { 
      //mydb.obj_sqlconnection.ConnectionString = ConfigurationManager.ConnectionStrings["cesConnectionString"].ToString(); 
      mydb.OpenConnection(); 
      rowsaffected = mydb.obj_sqlcommand.ExecuteNonQuery(); 
     } 
     finally 
     { 
      mydb.CloseConnection(); 
      mydb.obj_sqlcommand.Dispose(); 
     } 
     return rowsaffected; 
    } 
    return 0; 
} 
+0

請說明你如何使用這個類的方法 – Steve

+0

我已經更新了這篇文章。請看這裏 –

+0

從這段代碼我不能確定這個錯誤來自哪裏。你會在哪一行發生異常?發生異常時,您是否嘗試使用調試器來檢查連接的狀態? – Steve

回答

1

它過於複雜的解決方案外...這將解決您的理解和不必要的代碼行的問題:

解決方案:

namespace Stackoverflow 
{ 
    public static class Solution 
    { 
     static readonly string _connectionStringName = 
      @"mainConnectionStringName"; 

     static readonly string _connectionString = 
      _connectionStringName.getConnectionString(); 

     // string extended method like .ToLower() or .Trim() 
     public static string getConnectionString(
      this string connectionStringName) 
     { 
      return 
       System. 
       Configuration. 
       ConfigurationManager. 
       ConnectionStrings[connectionStringName]. 
       ConnectionString; 
     } 

     public static object SqlExecute(
      string connectionStringName, 
      string storedProcedureName, 
      System 
       .Collections 
       .Generic 
       .Dictionary<string, object> parameters, 
      bool isScalar) 
     { 
      object result = null; 

      using (System 
         .Data 
         .SqlClient 
         .SqlConnection connection = 
       new System.Data.SqlClient.SqlConnection(
        string.IsNullOrWhiteSpace(connectionStringName) 
         ? _connectionString 
         : connectionStringName.getConnectionString())) 
       if (connection != null) 
        using (System.Data.SqlClient.SqlCommand command = 
         new System.Data.SqlClient.SqlCommand() 
         { 
          CommandText = storedProcedureName, 
          CommandType = System 
              .Data 
              .CommandType 
              .StoredProcedure, 
          Connection = connection 
         }) 
         if (command != null) 
         { 
          if (parameters != null) 
           foreach (System 
              .Collections 
              .Generic 
              .KeyValuePair<string, object> 
               pair in parameters) 
            command.Parameters.AddWithValue(
             pair.Key, pair.Value); 

          command.Connection.Open(); 

          result = isScalar 
           ? command.ExecuteScalar() 
           : command.ExecuteNonQuery(); 

          if (command.Connection.State == 
            System.Data.ConnectionState.Open) 
           command.Connection.Close(); 
         } 

      return result; 
     } 
    } 
} 

用法:

namespace SomeNamespace 
{ 
    public sealed class SomeClass 
    { 
     public int Example() 
     { 
      return (int)Stackoverflow 
       .Solution 
       .SqlExecute(
        @"anyConnectionStringName", // or null for main connection string 
        @"anyStoredProcedureName", 
        new System 
         .Collections 
         .Generic 
         .Dictionary<string, object>() 
        { 
         { @"field0", "value" }, 
         { @"field1", -1.5 }, 
         { @"field2", System.DateTime.Now }, 
         { @"field3", 3.5 }, 
         { @"field4", 7 }, 
        }, 
        false // for ExecuteNonQuery or true for ExecuteScalar 
       ); 
     } 
    } 
} 
相關問題