2013-07-11 56 views
0

我试图从我的default_information.aspx.cs页面中选择一个用户,并在我已经创建注册表单的我的registration.aspx页面上显示该用户信息。[System.NullReferenceException:对象引用未设置为对象实例]

我得到System.NullReferenceException:Object reference not set to an instance of an object错误。请帮帮我。我已经给出了它的主要部分。我调试了它。我发现每个数据都从我的数据库中选择字符串strusername,strpassword。但是当我尝试在该文本字段上显示用户名或密码时,代码在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; 
    } 
+0

在至极线你恼火的错误? –

+0

检查objDataset.Tables [0] .Rows.Count> 0 –

+1

是否'usernametxt'为null?尝试在该行上放置一个断点。 – sq33G

回答

0

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

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

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

希望这会有所帮助。

0

您应该检查这条线

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

您试图访问直接objDataset.Tables[0],如果有什么与提供的用户名,以这种方法getalluser_info(string username),将dataset填写表格的用户。

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

希望这有助于

0

好吧,我已经找到了解决办法...我传递拨错way..here Web表单之间的数据是帮助我的链接:http://dotnetslackers.com/community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx 这里是解决

default_information的.aspx包含

保护无效gridviewprofile_SelectedIndexChanged(对象发件人,EventArgs的)

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

注册。ASPX包含:

保护无效的Page_Load(对象发件人,EventArgs的)

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

公共无效displayuser(用户名字符串)

 { 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;    
    } 
相关问题