代码如下,当我填TextBox好点Button后,提示错误:未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 77: SqlConnection myConn = new SqlConnection(System.Configuration.ConfigurationSettings弄了一天都没弄好,高手指点迷津。辛苦了,谢谢!!!!!!!!!!!using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;namespace 第n次
{
   public class WebForm2 : System.Web.UI.Page
    {
      protected System.Web.UI.WebControls.Label Label1;
      protected System.Web.UI.WebControls.Label Label2;
      protected System.Web.UI.WebControls.Label Label3;
      protected System.Web.UI.WebControls.Label Label4;
      protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator2;
     protected System.Web.UI.WebControls.Label Label5;
     protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
     protected System.Web.UI.WebControls.TextBox nameTextBox;
     protected System.Web.UI.WebControls.TextBox PasswordTextBox;
     protected System.Web.UI.WebControls.TextBox PasswordagainTextBox;
     protected System.Web.UI.WebControls.TextBox addressTextBox;
     protected System.Web.UI.WebControls.TextBox emailTextBox;
     protected System.Web.UI.WebControls.Button Button1;
     protected System.Web.UI.WebControls.Panel InputPanel;
     protected System.Web.UI.WebControls.Label Label6;
     protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
     private static string strConn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionStr"]; private void Page_Load(object sender, System.EventArgs e)
    {         
         
     }    #region Web 窗体设计器生成的代码
     override protected void OnInit(EventArgs e)
     {
       //
       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
       //
        InitializeComponent();
         base.OnInit(e);
      }

  private void InitializeComponent()
     {    
     this.Button1.Click += new System.EventHandler(this.Button1_Click);
      this.Load += new System.EventHandler(this.Page_Load);      }
     #endregion    private void Button1_Click(object sender, System.EventArgs e)
     {
       SaveNewUser();
      }
     private void SaveNewUser()
      {
         // 连接字符串和SQL语句
string Sql = "insert into userdata (name,Password,address,email) values" + " (@s_name,@s_Password,@s_address,@s_email)";
         string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionStr_kang"]; // 创建Connection和Command对象
SqlConnection myConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectString"].ToString());
SqlCommand myCommand = myConn.CreateCommand();
           // 初始化Command对象
   myCommand.CommandText = Sql;
    myCommand.CommandType = CommandType.Text;
             myCommand.Parameters.Add("@s_name",SqlDbType.NVarChar, 10);
    myCommand.Parameters["@s_name"].Value =nameTextBox.Text;
              myCommand.Parameters.Add("@s_Password", SqlDbType.NVarChar, 10);
               myCommand.Parameters["@s_Password"].Value = PasswordTextBox.Text;
               myCommand.Parameters.Add("@s_email", SqlDbType.NVarChar, 50);
                myCommand.Parameters["@s_email"].Value = emailTextBox.Text;
       myCommand.Connection.Open();
            if(1 == myCommand.ExecuteNonQuery())
      {
         // 新增成功,隐藏输入用户信息的面板,显示“输入成功”的面板
Response.Write("添加成功");

}
      else
        {
                  Response.Write ("添加失败");

         }

}

解决方案 »

  1.   

    不好意思 源错误应是: 行 77:SqlConnection myConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectString"].ToString());
      

  2.   

    你的配置文件贴来。
    有ConnectString嘛》???
      

  3.   

    未将对象引用设置到对象的实例ConnectString  存在问题
      

  4.   

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
         <appSettings>
        <add key="ConnectionSqlServer " value="Server=(local);User id=sa;Pwd=;Database=Northwind"></add>
    <add key="ConnectionSqlServer1" value="Server=(local);User id=sa;Pwd=;"></add>
    <add key="ConnectionSqlServer_tempdb" value="Server=(local);User id=sa;Pwd=;Database=tempdb"></add>
    <add key="ConnectionSqlServer_pubs" value="Server=(local);User id=sa;Pwd=;Database=pubs"></add>
    <add key="ConnectionDB2" value="DATABASE=SAMPLE;UID=username;PWD=password"></add>
    <add key="ConnectionOracle" value="Data Source=Oracle8i;Integrated Security=yes"></add>
      </appSettings> 
      <system.web>    <!--  动态调试编译
              设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为
              false 将提高此应用程序的运行时性能。
              设置 compilation debug="true" 以将调试符号(.pdb 信息)
              插入到编译页中。因为这将创建执行起来
              较慢的大文件,所以应该只在调试时将此值设置为 true,而在所有其他时候都设置为
              false。有关更多信息,请参考有关
              调试 ASP.NET 文件的文档。
      

  5.   

    你的配置文件中没有为ConnectString的Key啊,改一个
      

  6.   

    web.Config<configuration>
             <appSettings>
    <add key="ConnectString" value="server=.;uid=sa;pwd=你的密码;database=你的数据库"/>
    </appSettings>
      <system.web>
    ....
      

  7.   

    应该是 ConnectString 的问题