& # 39; System.NullReferenceException& # 39;初始化ConnectionMan

本文关键字:ConnectionMan 初始化 NullReferenceException System | 更新日期: 2023-09-27 18:06:09

我试图初始化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>

这是异常细节

系统。NullReferenceException未处理HResult=-2147467261
消息=对象引用未设置为对象的实例。
源= WindowsFormsApplication1加:C:'Users'user'Documents'Visual Studio . at WindowsFormsApplication1.ShowVoc. tor(2015 ' ' WindowsFormsApplication1 ' WindowsFormsApplication1项目' ShowVoc.cs:行24在WindowsFormsApplication1.main。voc_Click_1(对象发送方,EventArgs e) in C:'Users'user'Documents'Visual Studio2015 ' ' WindowsFormsApplication1 ' WindowsFormsApplication1项目' c:行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 (Message&m,鼠标按钮,点击32下)在System.Windows.Forms.Control.WndProc (Message&米)在System.Windows.Forms.ButtonBase.WndProc (Message&米)在System.Windows.Forms.Button.WndProc (Message&米)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message&米)在System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message&米)在System.Windows.Forms.NativeWindow。DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG&味精)System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtrdwComponentID, Int32 reason, Int32 pvLoopData)System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32原因,ApplicationContext上下文)System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32原因,ApplicationContext上下文)在System.Windows.Forms.Application。运行(mainForm形式)windows forms application . program . main () in C:'Users'user'Documents'Visual Studio2015 ' ' WindowsFormsApplication1 ' WindowsFormsApplication1项目' Program.cs:行19在System.AppDomain。_nExecuteAssembly(运行时汇编程序集,字符串[]args)在System.AppDomain。nExecuteAssembly(运行时汇编程序集,字符串[]args)在System.Runtime.Hosting.ManifestRunner。运行(布尔checkAptModel)在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly ()System.Runtime.Hosting.ApplicationActivator.CreateInstance (ActivationContextactivationContext, String[] activationCustomDataSystem.Runtime.Hosting.ApplicationActivator.CreateInstance (ActivationContextactivationContext)在System.Activator。调用CreateInstance (ActivationContext ActivationContext)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone ()在System.Threading.ThreadHelper。ThreadStart_Context(对象状态)System.Threading.ExecutionContext.RunInternal (ExecutionContextexecutionContext, ContextCallback, callback,对象状态,布尔值preserveSyncCtx)在System.Threading.ExecutionContext。运行ExecutionContext ExecutionContext, ContextCallback callback,对象状态,布尔值preserveSyncCtx)在System.Threading.ExecutionContext。运行(ExecutionContext, ExecutionContext, ContextCallback, callback, Object state)at System.Threading.ThreadHelper.ThreadStart() InnerException:

PS:我已经添加了Reference to system . configuration

& # 39; System.NullReferenceException& # 39;初始化ConnectionMan

在你的connectionstring中,我看到了一个不必要的;最后。我觉得你不需要那个。正确的版本应该是:

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

今后在提出新问题之前,请使用Google和SO自己的搜索引擎搜索您的问题。系统。NullReferenceException在各处都有详细的解释。要解决这个问题,请更改为

public ShowVoc()
        { string connectionString="";
            InitializeComponent();
            connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString;"].ConnectionString;
        }

这难道不是连接字符串的键输入错误的问题吗?我看到您在末尾添加了一个分号,这在XML中是不存在的。删除后应该没问题。