0

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

我收到System.NullReferenceException:Object reference not set to an instance of an object错误。请帮我。我已经给出了它的主要部分。我调试了它。我发现每个数据都是从我的数据库中选择的string 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;
     }
4

3 回答 3

0

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

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

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

希望这会有所帮助。

于 2013-07-11T12:59:59.493 回答
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(对象发送者,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);
    }

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

     {   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;            
    }
于 2013-07-13T20:23:41.437 回答
0

你应该检查这一行

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

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

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

希望这可以帮助

于 2013-07-13T11:12:45.933 回答