日期:2014-05-20  浏览次数:20744 次

linq中怎么解决这个问题:Cannot add an entity with a key that is already in use.
错误信息:Cannot add an entity with a key that is already in use.
错误源:App_Web_vvfzwtzj
堆栈跟踪: 在 UserManagerUC.imgBtnSubmit_Click(Object sender, ImageClickEventArgs e) 位置 d:\MyWork\IM\Controls\UserManagerUC.ascx.cs:行号 361


C# code


  protected void imgBtnSubmit_Click(object sender, ImageClickEventArgs e)

  {

  this.DoInit();

  try

  {

  DBIMDataContext IM = (DBIMDataContext)Application["IM"];

   

  try

  {

  var users = from user in IM.Users

  where user.userID == this.txtUserID.Text

  select user;

 

  if (users.Count<User>()>0)

  {

  this.lblFailed.Text = "This user has been existed!";

  this.lblFailed.Visible = true;

  return;

  }

  if (string.IsNullOrEmpty(this.txtAddress.Text))

  {

  this.lblFailed.Visible = true;

  this.lblFailed.Text = "Address is empty!";

  }

  else if (string.IsNullOrEmpty(this.txtCompany.Text))

  {

  this.lblFailed.Visible = true;

  this.lblFailed.Text = "Company is empty!";

  }

  else if (string.IsNullOrEmpty(this.txtEmail.Text))

  {

  this.lblFailed.Visible = true;

  this.lblFailed.Text = "Email is empty!";

  }

  else if (string.IsNullOrEmpty(this.txtPassword.Text))

  {

  this.lblFailed.Visible = true;

  this.lblFailed.Text = "Password is empty!";

  }

  else if (string.IsNullOrEmpty(this.txtTelphone.Text))

  {

  this.lblFailed.Visible = true;

  this.lblFailed.Text = "Telphone is empty!";

  }

  else

  {

  User xUser = new User();

  xUser.userID = this.txtUserID.Text;

  xUser.address = this.txtAddress.Text;

  xUser.company = this.txtCompany.Text;

  xUser.email = this.txtEmail.Text;

  if (string.IsNullOrEmpty(this.txtNickName.Text))

  {

  xUser.nickName = this.txtUserID.Text;

  }

  else

  {

  xUser.nickName = this.txtNickName.Text;

  }

  xUser.orderFlag = int.Parse(this.ddlOrderFlag.SelectedValue);

  xUser.password = this.txtPassword.Text;