SQL System.NullReferenceException

sta*_*tor -2 c# sql ext.net

我写了一个ac#函数来填充一个ext.net商店.它在一个应用程序中工作正常,但相同的代码在另一个应用程序中不起作用.我正在System.NullReferenceException上线26.这是第26行:

MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Run Code Online (Sandbox Code Playgroud)

这是我的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();
}
Run Code Online (Sandbox Code Playgroud)

为简化起见,我将替换将由硬编码的字符串传递的查询字符串"select id, name from b2b_group".

我似乎无法弄清楚为什么它会给出一个nullReferenceException,尤其是因为我在另一个项目中使用相同的代码.

我知道我正在监督一些小事,有人能发现吗?

非常感谢!

Fab*_*bio 5

配置文件中不存在连接字符串"MyConnectionString".检查connectionStrings部分.