public ActionResult CreateInspection() { string moldcode = Request.Form["hdmoldcode"]; using (var dbin = new MoldControlEntities()) { dbin.Database.Connection.Open(); try { var cav = Request.Form["hdcavityno"]; string[] key = Request.Form.GetValues("inspKey"); string[] result = Request.Form.GetValues("inspResult"); string[] desc = Request.Form.GetValues("inspDesc"); for (int i = 0; i < key.Length; i++) { td_inspection insp = new td_inspection(); insp.mold_code = moldcode; insp.cavity_no = cav; insp.inspection_id = byte.Parse(key[i]); insp.result = result[i] == "OK" ? true : false; insp.description = desc[i]; dbin.td_inspection.Add(insp); } dbin.SaveChanges(); } catch (Exception) { TempData["ErrorMessage"] = ""; } finally { dbin.Database.Connection.Close(); dbin.Dispose(); } } return(RedirectToAction("AddLedger", new { mold_code = moldcode })); }
public ActionResult CreateLedger(HttpPostedFileBase piccode, HttpPostedFileBase picmold) { string moldcode = Request.Form["moldcode"]; using (TransactionScope tran = new TransactionScope()) { using (var dbin = new MoldControlEntities()) { dbin.Database.Connection.Open(); try { DateTime dt = DateTime.Now; var cav = Request.Form["cavityno"]; if (!dbin.td_ledger.Any(w => w.mold_code == moldcode && w.cavity_no == cav)) { td_ledger ledger = new td_ledger(); ledger.mold_code = moldcode; ledger.cavity_no = cav; ledger.qty = int.Parse(Request.Form["qty"].ToString()); ledger.receive_date = convert.DateDisplayToDB(Request.Form["adddate"]); ledger.invoice_no = Request.Form["invoiceno"]; ledger.description = Request.Form["description"]; ledger.source = Request.Form["source"]; ledger.result = Request.Form["result"]; ledger.price = !string.IsNullOrEmpty(Request.Form["price"].ToString()) ? int.Parse(Request.Form["price"].ToString()) : 0; ledger.check_by = Session["MCS_Authen"].ToString(); ledger.create_dt = dt; ledger.status_id = 1; // Add data to DB TD_File // string subPath = "~/UploadFiles/" + moldcode + "/" + cav + "/"; if (!Directory.Exists(Server.MapPath(subPath))) { Directory.CreateDirectory(Server.MapPath(subPath)); } if (piccode != null && piccode.ContentLength > 0) { if (piccode.ContentType.Contains("image")) { var extension = Path.GetExtension(piccode.FileName); var fileName = "moldcode" + extension; var path = Path.Combine(Server.MapPath(subPath), fileName); piccode.SaveAs(path); ledger.pic_code = subPath + fileName; } } if (picmold != null && picmold.ContentLength > 0) { if (picmold.ContentType.Contains("image")) { var extension = Path.GetExtension(picmold.FileName); var fileName = "mold" + extension; var path = Path.Combine(Server.MapPath(subPath), fileName); picmold.SaveAs(path); ledger.pic_mold = subPath + fileName; } } dbin.td_ledger.Add(ledger); //string[] key = Request.Form.GetValues("insKey"); //string[] result = Request.Form.GetValues("insResult"); //string[] desc = Request.Form.GetValues("insDesc"); //for (int i = 0; i < key.Length; i++) //{ // td_inspection insp = new td_inspection(); // insp.mold_code = moldcode; // insp.cavity_no = cav; // insp.inspection_id = byte.Parse(key[i]); // insp.result = result[i] == "OK" ? true : false; // insp.description = desc[i]; // dbin.td_inspection.Add(insp); //} dbin.SaveChanges(); tran.Complete(); } } catch (Exception) { tran.Dispose(); TempData["ErrorMessage"] = ""; } finally { dbin.Database.Connection.Close(); dbin.Dispose(); tran.Dispose(); } } } return(RedirectToAction("AddLedger", new { mold_code = moldcode })); }