【发布时间】:2017-01-16 12:04:55
【问题描述】:
我正在尝试在
中初始化 ConnectionManager public ShowVoc()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString;"].ConnectionString;
}
但每当我运行它时都会给我“System.NullReferenceException”
这是应用配置代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication1.Properties.Settings.splaceConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\splace.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="WindowsFormsApplication1.Properties.Settings.VocConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Voc.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
这是异常详情
System.NullReferenceException 未处理 HResult=-2147467261
Message=对象引用未设置为对象的实例。
源 = WindowsFormsApplication1 堆栈跟踪: 在 C:\Users\user\Documents\Visual Studio 中的 WindowsFormsApplication1.ShowVoc..ctor() 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\ShowVoc.cs:line 24 在 C:\Users\user\Documents\Visual Studio 中的 WindowsFormsApplication1.main.voc_Click_1(Object sender, EventArgs e) 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\main.cs:line 53 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 原因, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 C:\Users\user\Documents\Visual Studio 中的 WindowsFormsApplication1.Program.Main() 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.Runtime.Hosting.ManifestRunner.Run(布尔 checkAptModel) 在 System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext 激活上下文,字符串 [] 激活自定义数据) 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext 激活上下文) 在 System.Activator.CreateInstance(ActivationContext 激活上下文) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调, 对象状态, Boolean 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:
PS:我已经添加了对 System.configration 的引用
【问题讨论】:
标签: c# database visual-studio nullreferenceexception