2014-10-06 88 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

相关问题