首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp.net 程序中,为什么把一条数据插入sql server数据库中,会连续两次把相同一条数据插入到表中解决方案

2012-04-21 
asp.net 程序中,为什么把一条数据插入sql server数据库中,会连续两次把相同一条数据插入到表中public Acti

asp.net 程序中,为什么把一条数据插入sql server数据库中,会连续两次把相同一条数据插入到表中
public ActionResult SelectById(ModelBuyIndent MB, FormCollection f)
  {
  btn.Add(new ModelButton { btnPower = "W", btnText = "入库", btnClass = "", btnFunction = "toSubmit(this)", btnName = "Sav", btnId = "Sav" });
  ViewData["ButList"] = new SDSButton().GetButton("DetStorage", "Stock", "", btn);
  //根据采购订单ID获取数据
  ModelBuyIndent Lk = BS.GetAllStock(MB.Id);
  foreach (var item1 in Lk.BuyIndentList)
  {
  if (item1.Num > item1.StockPNum)
  {
  //根据ID获取仓库名
  Storage S = BS.SelectByIdStorage(MB.Storage.StorageId);
  #region 添加库存单据表
  StockBill sb = new StockBill();
  sb.SBillType = 1;
  sb.SupplierId = Lk.SupplierId;
  sb.SupplierName = Lk.SupplierName;
  sb.SBillDate = MB.Time;
  sb.SBillTotal = decimal.Parse(f["zh"].ToString());
  sb.RecordId = (User.Identity as SDSIdentity).ID;
  sb.RecordName = (User.Identity as SDSIdentity).UName;
  sb.RecordTime = DateTime.Now.ToLocalTime();
  sb.UpdateId = (User.Identity as SDSIdentity).ID;
  sb.UpdateName = (User.Identity as SDSIdentity).UName;
  sb.UpdateTime = DateTime.Now.ToLocalTime();
  StockBill TF = BS.AddStockBill(sb);
  new BllLog().Save("DetStorage", "Stock", "", 3, "DET_StockBill", "添加库存单据(单据编号:" + sb.SBillNo + ")");
  #endregion

  #region 添加库存
  if (TF.SBillId > 0)
  {
  //添加库存单的每条商品
  foreach (ModelBuyIndentList item in MB.BuyIndentList)
  {
  ModelBuyIndentList ll = Lk.BuyIndentList.First(m => m.Id == item.Id);
  if (ll.Num > ll.StockPNum)
  {
  List<Stock> ListS = BS.SelectAllStock(null, item.Id, null, null, null, null, null, null, 1);
  Stock s = new Stock();
  s.SBillId = TF.SBillId;
  s.StorageId = MB.Storage.StorageId;
  if (ListS.Count != 0)
  {
  s.StockBatch = (ListS.Count + 1).ToString();
  }
  else
  {
  s.StockBatch = "1";
  }
  s.StockNum += item.LNum;
  s.StockType = 1;
  s.OrderId = ll.BIndentId;
  s.ProductId = ll.ProductId;
  s.ProductNo = ll.ProductNo;
  s.ProductName = ll.ProductName;
  s.ProductUnit = ll.Product.ProductUnit;
  s.PTypeId = ll.Product.PTypeId;
  s.PBrandId = ll.Product.PBrandId;


  s.PBrandName = ll.Product.PBName;
  s.StockPrice = ll.Price;
  s.StockTotal = item.LNum * ll.Price;
  s.IsDel = false;
  s.RecordId = (User.Identity as SDSIdentity).ID;
  s.RecordName = (User.Identity as SDSIdentity).UName; ;
  s.RecordTime = DateTime.Now.ToLocalTime();
  s.UpdateId = (User.Identity as SDSIdentity).ID;
  s.UpdateName = (User.Identity as SDSIdentity).UName;
  s.UpdateTime = DateTime.Now.ToLocalTime();
  s.BCListId = ll.Id;
  bool ft = BS.AddStock(s);
  new BllLog().Save("DetStorage", "Stock", "", 3, "DET_StockBill", "添加库存(产品名:" + s.ProductName + ")");
  //添加库存表成功后更新订单表
  if (ft)
  {
  //入库后,自动将采购订货单状态改为:已到货
  UpdateStatu(item.Id);

  ModelBuyIndentList MIL = Lk.BuyIndentList.First(m => m.Id == item.Id); ;
  MIL.StockPNum = short.Parse((MIL.StockPNum + item.LNum).ToString());
  bool b = new BLLIndent().EditIndentListNum(MIL);
  new BllLog().Save("DetStorage", "Stock", "", 4, "DET_BuyIndentList", "修改采购单(订货ID:" + MIL.Id + ")");
  }
  }

  }
  }

  return RedirectToAction("Bill", "Stock", new { flag = 1 });
  }
  }
  #endregion

  //查询所有仓库
  ViewData["ListSt"] = BS.SelectAllStorage(null, null, null, null);
  return View(Lk);
  }

[解决办法]
仔细查查都什么地方调用了SelectById,是不是连续触发了,下个断点不就清楚了
[解决办法]
好乱啊。。。。
好好检查下是否回发的时候多次调用了这个方法。
[解决办法]
应该是连续触发了
[解决办法]
好乱 确定一下where后面的值是唯一不

热点排行