我已經瀏覽了我的代碼,看看是否存在空值,但是每個人都帶來了價值,但我找不到問題。錯誤對象引用未設置爲對象的實例?
public partial class UserDetail : ICSBaseUserControl
{
UserRepository userDao = new UserRepository();
User user;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
long userID = Convert.ToInt64(Request[UrlParameters.UrlParameterName.UserID]);
user = (userID > 0) ? userDao.GetUser(AppSession.Company.ID, userID) : new c365_EntityFramework.User();
if (user.ID > 0)
{
txtFirstName.Text = user.Forename;
txtSurname.Text = user.Surname;
txtAddress.Text = user.Address1;
txtEmail.Text = user.Email;
txtUsername.Text = user.Username;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
bool result = userDao.UpdateUser(user.ID, txtFirstName.Text, txtEmail.Text, txtSurname.Text, txtAddress.Text, txtUsername.Text);
}
}
編輯
這是我的固定它,通過移動user
裝載了if(!IsPostBack)
的:
public partial class UserDetail : ICSBaseUserControl
{
UserRepository userDao = new UserRepository();
User user;
protected void Page_Load(object sender, EventArgs e)
{
long userID = Convert.ToInt64(Request[UrlParameters.UrlParameterName.UserID]);
user = (userID > 0) ? userDao.GetUser(AppSession.Company.ID, userID) : new c365_EntityFramework.User();
if (!IsPostBack)
{
if (user.ID > 0) // Check for user ID
{
txtFirstName.Text = user.Forename;
txtSurname.Text = user.Surname;
txtAddress.Text = user.Address1;
txtEmail.Text = user.Email;
txtUsername.Text = user.Username;
}
}
}
哪一行是錯誤? – Paddyd
你在哪裏得到這個錯誤? –
bool result = userDao.UpdateUser(user.ID,txtFirstName.Text,txtEmail.Text,txtSurname.Text,txtAddress.Text,txtUsername.Text); – user2520671