【发布时间】:2014-10-06 14:25:09
【问题描述】:
我编写了一个 c# 函数来填充 ext.net 存储。它在一个应用程序中运行良好,但相同的代码在另一个应用程序中不起作用。我在第 26 行收到 System.NullReferenceException。这是第 26 行:
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,尤其是看到我在另一个项目中使用了相同的代码。
我知道我正在监督一些小事,有人能发现吗?
非常感谢!
【问题讨论】:
-
显然它找不到你的连接字符串。您是否在调试器中验证了这一点?显示
MyConnection.ConnectionString的值。显示您的 XML 是如何定义的。 -
嗯,在我看来
ConnectionStrings["MyConnectionString"]可能为空。您确定已将其添加到此新项目的配置中吗? -
就是这样!我忘记了我的连接字符串 -.- 我知道我犯了一个愚蠢的错误......
-
@JeroenVannevel 我读到了,但是是的,同意,它是重复的。