2013-07-11 86 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;    
    } 
相關問題