我正在尝试从我的default_information.aspx.cs
页面中选择一个用户,并在我已创建注册表单的registration.aspx
页面上显示该用户信息。
我收到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)
看起来你正在使用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;
}