[System.NullReferenceException:对象引用未设置为对象的实例。]

时间:2013-07-11 10:55:04

标签: asp.net webforms

我正在尝试从我的default_information.aspx.cs页面中选择一个用户,并在我已创建注册表单的registration.aspx页面上显示该用户信息。

我收到System.NullReferenceException:Object reference not set to an instance of an object错误。请帮我。我已经给出了它的主要部分。我调试了它。我发现每个数据都是从我的数据库中选择的字符串 strusernamestrpassword。但是,当我尝试在该文本字段上显示用户名或密码时,usernametxt.Text = strusername;上的代码会中断。

default_information包含

    protected void gridviewprofile_SelectedIndexChanged(object sender, EventArgs e)
    {
        registration objdef = new registration();
        string username = gridviewprofile.Rows[gridviewprofile.SelectedIndex].Cells[1].Text;
        objdef.displayuser(username);
    }
    protected void update_Click(object sender, EventArgs e)
    {
        Response.Redirect("registration.aspx");
    }

registration.aspx包含

    protected void register_Click(object sender, EventArgs e)
    {
        user objuser = new user();
        objuser.username = usernametxt.Text;
        objuser.password = passwordtxt.Text;
        objuser.email = emailtxt.Text;
        objuser.Save();
    }
    public void displayuser(string username)
     {   user obj = new user();
        DataSet objDataset = obj.profile(username);
        string strusername = objDataset.Tables[0].Rows[0][0].ToString();
        string strpassword = objDataset.Tables[0].Rows[0][1].ToString();
        string stremail = objDataset.Tables[0].Rows[0][2].ToString();      
        usernametxt.Text = strusername;
        passwordtxt.Text = strpassword;
        emailtxt.Text = stremail;            
    }

用户类包含

public class user
{    
    public void Save()
    {
        clssqlserver obj = new clssqlserver();
        obj.insertuser_info(Username,Password,Email);                           
    }
     public DataSet profile(string username)
    {
        clssqlserver obj = new clssqlserver();
        return obj.getalluser_info(username);
    }

}

clssqlserver包含

    public DataSet getalluser_info(string username)
    {
        string connectionstring = "Data Source=localhost\\mssql;Initial Catalog=blooddb;Integrated Security=True";
        SqlConnection objconnection = new SqlConnection(connectionstring);
        objconnection.Open();
        string command = "Select * from login_donor where username='" + username + "' ";
        SqlCommand objcommand = new SqlCommand(command, objconnection);
        DataSet objdataset = new DataSet();
        SqlDataAdapter objadapter = new SqlDataAdapter(objcommand);
        objadapter.Fill(objdataset);
        objconnection.Close();
        return objdataset;
    }
    public bool insertuser_info(string username,string password,string email)

      {     string connectionstring = "Data Source=localhost\\mssql;Initial Catalog=blooddb;Integrated Security=True";
            SqlConnection objconnection = new SqlConnection(connectionstring);
            objconnection.Open();
            string strInsertCommand = "insert into login_donor values('"+ username +"','"+ password + "','"+email+"')";
            SqlCommand objcommand = new SqlCommand(strInsertCommand, objconnection);
            objcommand.ExecuteNonQuery();
            objconnection.Close();
            return true;
     }

3 个答案:

答案 0 :(得分:0)

看起来你正在使用asp.net创建用户向导控件。因为你的控件被埋在另一个容器中,你必须在经过一些小挖掘之后得到奖励。让我们开始挖掘......

使用已经可访问的向导找到您的文本框

TextBox  usernametxt= (TextBox)CreateUserWizard.FindControl("usernametxt");
usernametxt.Text = strusername;

希望这会有所帮助。

答案 1 :(得分:0)

你应该检查这一行

string strusername = objDataset.Tables[0].Rows[0][0].ToString();

您尝试直接访问objDataset.Tables[0],如果没有用户使用此方法getalluser_info(string username)提供的用户名,dataset是否会填写表格。

您应首先检查dataset中是否有任何表格。

希望这会有所帮助

答案 2 :(得分:0)

我已经找到了解决方案......我正在以网络方式传递Webforms之间的数据......有一些链接可以帮助我:http://dotnetslackers.com/community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx 这是解决方案

default_information.aspx包含

protected void gridviewprofile_SelectedIndexChanged(object sender,EventArgs e)

    {   string username = gridviewprofile.Rows[gridviewprofile.SelectedIndex].Cells[1].Text;
        Response.Redirect("registration.aspx?id="+username);
    }

registration.aspx包含:

protected void Page_Load(object sender,EventArgs e)

    {   
        string queryStringID = Request.QueryString["id"];
        displayuser(queryStringID);
    }

public void displayuser(string username)

     {   user obj = new user();
        DataSet objDataset = obj.profile(username);
        string strusername = objDataset.Tables[0].Rows[0][0].ToString();
        string strpassword = objDataset.Tables[0].Rows[0][1].ToString();
        string stremail = objDataset.Tables[0].Rows[0][2].ToString();      
        usernametxt.Text = strusername;
        passwordtxt.Text = strpassword;
        emailtxt.Text = stremail;            
    }