请参阅以下代码:
Public Sub GetConnectionString(Dim strCon As String)
Dim stringtochange As String = ConfigurationManager.ConnectionStrings("GeniedbConnection").ConnectionString 'line 2
Dim configFile As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim connectionStringsSection As ConnectionStringsSection
connectionStringsSection = CType(configFile.GetSection("GeniedbConnection"), ConnectionStringsSection)
connectionStringsSection.ConnectionStrings("GeniedbConnection").ConnectionString = strCon 'Line 6
configFile.Save()
End Sub
我正在尝试更改app.config中的连接字符串。第2行返回当前连接字符串。第6行抛出异常:`“对象引用未设置为对象的实例。”这是为什么?
答案 0 :(得分:0)
第2行
Dim stringtochange As String = ConfigurationManager.ConnectionStrings("GeniedbConnection").ConnectionString
与第6行不同
connectionStringsSection.ConnectionStrings("GeniedbConnection").ConnectionString = strCon
ConfigurationManager!= connectionStringsSection
其中任何一个都为空,connectionStringsSection
,connectionStringsSection.ConnectionStrings("GeniedbConnection")