专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > LINQ

linq中如何解决这个有关问题:Cannot add an entity with a key that is already in use

发布时间:2011-06-24 21:55:23 文章来源:www.iduyao.cn 采编人员:星星草
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;
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: