[英]System.NullReferenceException in a class library
I'm trying to configure some connection strings in app.config, but keep getting this error message, 我正在尝试在app.config中配置一些连接字符串,但始终收到此错误消息,
System.NullReferenceException: Object reference not set to an instance of an object.
I've looked at numerous solutions on the web, but nothing directly applies, 我在网络上看过许多解决方案,但没有直接适用的方法,
System.Configuration
and am using ConfigurationManager
我引用了System.Configuration
并正在使用ConfigurationManager
The only difference is that the project is a class library, which I'm exposing to Excel via ExcelDNA. 唯一的区别是该项目是一个类库,我将通过ExcelDNA将其公开给Excel。
Is there something special I need to do to get this to work with Excel? 为了使此功能与Excel配合使用,我需要做些特别的事情吗?
Here's a snippet of the XML, 这是XML的代码段,
<configuration>
<connectionStrings>
<add name="ConnectionCSV" providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;" />
</connectionStrings>
</configuration>
This is the source code (have got a using System.Configuration
along with necessary reference), 这是源代码(已using System.Configuration
以及必要的参考),
string constr = ConfigurationManager.ConnectionStrings["ConnectionCSV"].ConnectionString;
The above line throws the Object reference not set to an instance of an object
error. 上一行抛出Object reference not set to an instance of an object
错误Object reference not set to an instance of an object
的Object reference not set to an instance of an object
。
The name of the configuration file needs to match the name of the .XLL file that is created by ExcelDNA. 配置文件的名称必须与ExcelDNA创建的.XLL文件的名称匹配。
eg 例如
MyApp-AddIn.xll
-> MyApp-AddIn.xll.config
MyApp-AddIn.xll
> MyApp-AddIn.xll.config
MyApp-AddIn64.xll
-> MyApp-AddIn64.xll.config
MyApp-AddIn64.xll
> MyApp-AddIn64.xll.config
app.config
files, they belong to the parent executable. 库没有自己的app.config
文件,它们属于父可执行文件。 app.config
file. Excel.exe没有其自己的app.config
文件。 ConnectionCSV
does not exist in whatever configuration file, if any, is being used at runtime. 连接字符串ConnectionCSV
在运行时使用的任何配置文件(如果有)中都不存在。 You can verify this by checking if ConfigurationManager.ConnectionStrings["ConnectionCSV"] != null
first and warning the user if the key doesn't exist. 您可以通过首先检查ConfigurationManager.ConnectionStrings["ConnectionCSV"] != null
并警告用户密钥是否不存在来验证这一点。 In your case it might be better to store the connection string in the user's registry instead. 在您的情况下,最好将连接字符串存储在用户的注册表中。 To whit: 白衣:
Update : 更新 :
I just found this page on MSDN which describes your exact problem and explains how you can use app.config
files with Office add-ins: https://msdn.microsoft.com/en-us/library/16e74h9c.aspx 我刚刚发现这个MSDN页面描述您的具体问题,并解释了如何使用app.config
文件与Office加载项: https://msdn.microsoft.com/en-us/library/16e74h9c.aspx
Specifically: 特别:
In the [app.config filename] box, type the same name as the assembly plus the extension .config. 在[app.config文件名]框中,键入与程序集相同的名称以及扩展名.config。 For example, a configuration file for an Excel project assembly called
ExcelWorkbook1.dll
would be namedExcelWorkbook1.dll.config
. 例如,一个名为ExcelWorkbook1.dll
的Excel项目程序集的配置文件将命名为ExcelWorkbook1.dll.config
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.