2014-10-06 93 views
-2

我寫了一個c#函數來填充ext.net商店。它在一個應用程序中正常工作,但相同的代碼在另一個應用程序中不起作用。我上線26這得到一個System.NullReferenceException是26行:SQL System.NullReferenceException

MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; 

這裏是我的C#功能:

protected void fillStore(Ext.Net.Store store, string query) 
{ 
    SqlDataReader MyReader; 
    SqlConnection MyConnection = new SqlConnection(); 
    MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; 

    SqlCommand fillCommand = new SqlCommand(); 
    fillCommand.CommandText = "select id, name from b2b_group"; 
    fillCommand.CommandType = CommandType.Text; 
    fillCommand.Connection = MyConnection; 

    fillCommand.Connection.Open(); 
    MyReader = fillCommand.ExecuteReader(CommandBehavior.CloseConnection); 

    store.DataSource = MyReader; 
    store.DataBind(); 

    fillCommand.Dispose(); 
    MyConnection.Dispose(); 
} 

爲了簡化起見,我替換可能會通過硬傳遞的查詢字符串編碼爲"select id, name from b2b_group"之一。

我似乎無法弄清楚爲什麼它給了nullReferenceException,特別是看到我有相同的代碼在另一個項目中工作。

我知道我正在監督一些小事情,有人能發現嗎?

非常感謝!

+2

顯然它找不到你的連接字符串。你是否在你的調試器中驗證了這一點?顯示MyConnection.ConnectionString的值。顯示你的XML是如何定義的。 – 2014-10-06 14:27:48

+0

在我看來''ConnectionStrings [「MyConnectionString」]'可能爲空。你確定你將它添加到這個新項目的配置中嗎? – pquest 2014-10-06 14:28:13

+0

就是這樣!我忘了我的連接字符串 - .-我知道我犯了一個愚蠢的錯誤... – starvator 2014-10-06 14:28:28

回答

5

連接字符串'MyConnectionString'不存在於您的配置文件中。檢查connectionStrings部分。

+0

我知道我忘記了這麼簡單的東西 - 謝謝! – starvator 2014-10-06 14:28:56

相關問題