' System.NullReferenceException'初始化ConnectionManager

时间:2016-09-08 13:57:31

标签: c# database visual-studio nullreferenceexception

我想在

中初始化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 =对象引用未设置为对象的实例   Source = WindowsFormsApplication1 StackTrace:          在C:\ Users \ user \ Documents \ Visual Studio中的WindowsFormsApplication1.ShowVoc..ctor()   2015年\项目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ ShowVoc.cs:行   24          at WindowsFormsApplication1.main.voc_Click_1(Object sender,EventArgs e)在C:\ Users \ user \ Documents \ Visual Studio中   2015年\项目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ main.cs:行   53          在System.Windows.Forms.Control.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在System.Windows.Forms.Control.WmMouseUp(消息&amp; m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息&amp; m)          在System.Windows.Forms.ButtonBase.WndProc(消息&amp; m)          在System.Windows.Forms.Button.WndProc(消息&amp; m)          在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)          在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(Form mainForm)          在C:\ Users \ user \ Documents \ Visual Studio中的WindowsFormsApplication1.Program.Main()   2015年\项目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \的Program.cs:行   19          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)          在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()          在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext   activationContext,String [] activationCustomData)          在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext   activationContext)          在System.Activator.CreateInstance(ActivationContext activationContext)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean   preserveSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()InnerException:

PS:我已经添加了对System.configration的引用

3 个答案:

答案 0 :(得分:0)

将来,在提出新问题之前,请先使用Google和SO自己的搜索引擎搜索您的问题。 System.NullReferenceException在整个地方都有详细的解释。要解决您的问题,请转到

public ShowVoc()
        { string connectionString="";
            InitializeComponent();

            connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString;"].ConnectionString;
        }

答案 1 :(得分:0)

在你的连接字符串中,我看到一个不需要的;在末尾。我想你不需要那个。正确的版本应该是:

connectionString = ConfigurationManager
    .ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString"]
    .ConnectionString;

答案 2 :(得分:0)

难道只是连接字符串中的拼写错误?我看到你在结尾添加了一个分号,这在XML中不存在。删除它应该没问题。