说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 39:         protected void CreateUserButton_Click(object sender, EventArgs e)
行 40:         {
行 41:             string username = ((TextBox)this.FindControl("UserName")).Text;
行 42:             string psw = ((TextBox)this.FindControl("Password")).Text;
行 43:             string conpsw = ((TextBox)this.FindControl("ConfirmPassword")).Text;
 源文件: C:\Users\Administrator\Desktop\毕业设计\程序\biyesheji\biyesheji\Account\register.aspx.cs    行: 41 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   biyesheji.Account.Register.CreateUserButton_Click(Object sender, EventArgs e) in C:\Users\Administrator\Desktop\毕业设计\程序\biyesheji\biyesheji\Account\register.aspx.cs:41
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 
原。cs文件,红色字体部分出现问题
using System;
using System.Collections.Generic;using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;using System.Configuration;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;
namespace biyesheji.Account
{
    public partial class Register : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }
        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            string username = ((TextBox)this.FindControl("UserName")).Text;
            string psw = ((TextBox)this.FindControl("Password")).Text;
            string conpsw = ((TextBox)this.FindControl("ConfirmPassword")).Text;
            string id = ((TextBox)this.FindControl("Email")).Text;
            string name = ((TextBox)this.FindControl("Name")).Text;
            string sex = "女";
            string brith = ((TextBox)this.FindControl("Brith")).Text;
            string add = ((TextBox)this.FindControl("Addr")).Text;
            string phonenum = ((TextBox)this.FindControl("TextBox1")).Text;
            string telnum = ((TextBox)this.FindControl("TextBox2")).Text;
            RadioButton a = (RadioButton)this.FindControl("RadioButton2");
            if (a.Checked == true) sex = "男";            Session["name"] = name;
            Session["id"] = name;            SqlConnection con = dbhelper.ceratcon();
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into login(log_username,log_psw,ID,log_name,log_sex,log_birth,log_phonenum,log_telnum,log_addr) values("+username+","+psw+","+id+","+name+","+sex+","+brith+","+phonenum+","+telnum+","+add+")"); 
            SqlDataReader sdr = cmd.ExecuteReader();
            con.Close();
        }    }
}

解决方案 »

  1.   

    string username = ((TextBox)this.FindControl("UserName")).Text;没有找到TextBox这个控件
      

  2.   

    因为你使用的是wizard,所以直接这样string username = RegisterUser.UserName;
    string psw =RegisterUser.Password;
    ...其他类似
      

  3.   

    this.FindControl这里的this是什么?是Button?你到按钮里面去找控件?
      

  4.   

    string username = UserName.Text;
    不行么?