我前台用了一个CreateUserWizard控件,Web.config配置文件中什么都没配。
这是我注册后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.Services.Description;  
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }    protected void CreateUserWizard1_CreatedUser1(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=" + Server.MapPath("App_Data//db1.mdb");
        OleDbCommand comm = new OleDbCommand();
        comm.Connection = conn;
   
         comm.Parameters.AddWithValue("@UserName", ((TextBox)this.CreateUserWizardStep1.FindContro("UserName")).Text);
        conn.Open();
        int i = (int)comm.ExecuteScalar();        if (i > 0)
        {            string UserNamee = ((TextBox)this.CreateUserWizard1.FindControl("UserName")).Text;
            string UserPassword = ((TextBox)this.CreateUserWizard1.FindControl("Password")).Text;
            string Conpassword = ((TextBox)this.CreateUserWizard1.FindControl("ConfirmPassword")).Text;
            string Eail = ((TextBox)this.CreateUserWizard1.FindControl("Email")).Text;
            string Question = ((TextBox)this.CreateUserWizard1.FindControl("Question")).Text;
            string Answer = ((TextBox)this.CreateUserWizard1.FindControl("Answer")).Text;
            comm.CommandText = "insert into zzg1(UserName,UserPassword,Conpassword,Eail,Question,Answer) values('" + UserNamee + "','" + UserPassword + "','" + Conpassword + "','" + Eail + "','" + Question + "','" + Answer + "')";
            comm.ExecuteNonQuery();
        }
        else
        {            Response.Write("<script>alert('sdasda')</script>");        }    }
}调试了,可是为什么程序到了 comm.Parameters.AddWithValue("@UserName", ((TextBox)this.CreateUserWizardStep1.FindContro("UserName")).Text); 这,他老出未将对象引用设置到对象的实例的错误呀,弄了许多次都不行,到底是怎么回事呢 ?

解决方案 »

  1.   

    ((TextBox)this.CreateUserWizardStep1.FindContro("UserName")).Text);这样的问题,自己调试一下,不就很容易找到原因了?
    未将对象引用设置到对象的实例
    很明显有用了 null 对象(TextBox)this.CreateUserWizardStep1.FindContro("UserName")) 是 null最好是:
    TextBox txt = this.CreateUserWizardStep1.FindContro("UserName") as TextBox;
    if(txt!=null){
    //使用txt
    }
      

  2.   

    TextBox txt = this.CreateUserWizardStep1.FindContro("UserName") as TextBox;
    if(txt!=null)
    {
        //代码
    }
      

  3.   

    try
    {
      TextBox txt = this.CreateUserWizardStep1.FindContro("UserName") as TextBox;
    }
    catch
    {}
    空对象异常不处理 ,程序继续向下运行
      

  4.   

    你的那个TEXTBOX没有找到,才出现了这样的问题。
      

  5.   

    未将对象引用设置到对象的实例在出现这种问题的时候,你先要做判断 !=null  来做,有时为null那么就出错。
      

  6.   

    我想提取CreateUserWizard 控件中的UserName 的值,具体的我该怎么写的?