connectionString = WebConfigurationManager.ConnectionStrings["sw111"].ConnectionString;
这里调试之后会显示:未将对象引用设置到对象的实例。说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 18:     public UserDB()
行 19:     {
行 20:         connectionString = WebConfigurationManager.ConnectionStrings["sw111"].ConnectionString;
行 21:     }
请哪位大虾会的指导指导啊~

解决方案 »

  1.   

    说明WebConfigurationManager.ConnectionStrings["sw111"]没找到
      

  2.   

    ConnectionStrings["sw111"]
    看看sw111是不是不正确.
      

  3.   

    WebConfigurationManager怎么定义的,是否引用
    ConfigurationManager.ConnectionStrings["sw111"]
    config是否设置sw111
      

  4.   

    web.config 里没有设置name为sw111的连接字符串,所以找不到
      

  5.   

    把你webconfig里面的代码贴出来就知道啦
      

  6.   

    web.config 里有没有那个节点。
      

  7.   

    WebConfigurationManager的定义using System;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Runtime;namespace System.Web.Configuration
    {
        // 摘要:
        //     在配置文件应用于 Web 应用程序时提供对配置文件的访问。
        public static class WebConfigurationManager
        {
            // 摘要:
            //     获取当前 Web 应用程序的默认配置的 System.Configuration.AppSettingsSection 数据。
            //
            // 返回结果:
            //     一个 System.Collections.Specialized.NameValueCollection 对象,它包含当前 Web 应用程序的默认配置的
            //     System.Configuration.AppSettingsSection 对象。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能使用该应用程序设置数据检索到有效的 System.Collections.Specialized.NameValueCollection 对象。
            public static NameValueCollection AppSettings { get; }
            //
            // 摘要:
            //     获取当前 Web 应用程序的默认配置的 System.Configuration.ConnectionStringsSection 数据。
            //
            // 返回结果:
            //     一个 System.Configuration.ConnectionStringSettingsCollection 对象,它包含当前 Web 应用程序的默认配置的
            //     System.Configuration.ConnectionStringsSection 对象的内容。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能检索到有效的 System.Configuration.ConnectionStringSettingsCollection 对象。
            public static ConnectionStringSettingsCollection ConnectionStrings { get; }        // 摘要:
            //     从当前 Web 应用程序的配置文件中检索指定的配置节。
            //
            // 参数:
            //   sectionName:
            //     配置节名称。
            //
            // 返回结果:
            //     指定的配置节对象或 null(如果该节不存在)。请记住,将 System.Web.Configuration.WebConfigurationManager.GetSection(System.String)
            //     用作运行时操作存在安全限制。例如,您可能无法在运行时访问某节以进行修改。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            public static object GetSection(string sectionName);
            //
            // 摘要:
            //     从指定的 Web 应用程序的配置文件中检索指定的配置节。
            //
            // 参数:
            //   sectionName:
            //     配置节名称。
            //
            //   path:
            //     虚拟配置文件路径。
            //
            // 返回结果:
            //     指定的配置节对象或 null(如果该节不存在)。请记住,将 System.Web.Configuration.WebConfigurationManager.GetSection(System.String,System.String)
            //     用作运行时操作存在安全限制。例如,您可能无法在运行时访问某节以进行修改。
            //
            // 异常:
            //   System.InvalidOperationException:
            //     从 Web 应用程序外调用方法。
            //
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            public static object GetSection(string sectionName, string path);
            //
            // 摘要:
            //     从当前 Web 应用程序的配置文件中检索指定的配置节。
            //
            // 参数:
            //   sectionName:
            //     配置节名称。
            //
            // 返回结果:
            //     指定的配置节对象,或 null(如果该节不存在),或某个内部对象(如果该节不能在运行时访问)。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            public static object GetWebApplicationSection(string sectionName);
            //
            // 摘要:
            //     将当前计算机上的计算机配置文件作为 System.Configuration.Configuration 对象打开以允许读或写操作。
            //
            // 返回结果:
            //     一个 System.Configuration.Configuration 对象。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            public static System.Configuration.Configuration OpenMachineConfiguration();
            //
            // 摘要:
            //     将当前计算机上的计算机配置文件作为 System.Configuration.Configuration 对象打开以允许读或写操作。
            //
            // 参数:
            //   locationSubPath:
            //     应用计算机配置的应用程序路径。
            //
            // 返回结果:
            //     一个 System.Configuration.Configuration 对象。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath);
            //
            // 摘要:
            //     将指定服务器上的指定计算机配置文件作为 System.Configuration.Configuration 对象打开以允许读或写操作。
            //
            // 参数:
            //   locationSubPath:
            //     配置应用的应用程序路径。
            //
            //   server:
            //     完全限定服务器名,将返回该服务器的配置。
            //
            // 返回结果:
            //     一个 System.Configuration.Configuration 对象。
            //
            // 异常:
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath, string server);
            //
            // 摘要:
            //     使用指定的安全上下文将指定服务器上的指定计算机配置文件作为 System.Configuration.Configuration 对象打开以允许读或写操作。
            //
            // 参数:
            //   locationSubPath:
            //     配置应用的应用程序路径。
            //
            //   server:
            //     完全限定服务器名,将返回该服务器的配置。
            //
            //   userToken:
            //     要使用的帐户标记。
            //
            // 返回结果:
            //     一个 System.Configuration.Configuration 对象。
            //
            // 异常:
            //   System.ArgumentException:
            //     没有为 server 或 userToken 参数提供有效值。
            //
            //   System.Configuration.ConfigurationErrorsException:
            //     未能加载有效的配置文件。
            [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
            public static System.Configuration.Configuration OpenMachineConfiguration(string locationSubPath, string server, IntPtr userToken);