首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

运行报错,迷津

2012-01-01 
运行报错,高手指点迷津代码如下,当我填TextBox好点Button后,提示错误:未将对象引用设置到对象的实例。说明:

运行报错,高手指点迷津
代码如下,当我填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   ( "添加失败 ");

                  }

}



[解决办法]
你的配置文件贴来。
有ConnectString嘛》???
[解决办法]
自己解决了更好
[解决办法]
未将对象引用设置到对象的实例

ConnectString 存在问题
[解决办法]
你的配置文件中没有为ConnectString的Key啊,改一个
[解决办法]
WY
[解决办法]
web.Config

<configuration>
<appSettings>
<add key= "ConnectString " value= "server=.;uid=sa;pwd=你的密码;database=你的数据库 "/>
</appSettings>
<system.web>
....
[解决办法]
</configuration> 没有.

[解决办法]
应该是 ConnectString 的问题
[解决办法]
你最好贴贴你的web.config文件

热点排行