private void ImportData() { try { using (DataClasses1DataContext db = new DataClasses1DataContext()) { foreach (DataRow rd in dt.Rows) { if (!rd["UnitCode"].ToString().Equals("")) { var x = (from ix in db.tb_Units where ix.UnitCode.ToLower().Trim() == rd["UnitCode"].ToString().ToLower().Trim() select ix).FirstOrDefault(); if (x == null) { tb_Unit ts = new tb_Unit(); ts.UnitCode = Convert.ToString(rd["UnitCode"].ToString()); ts.UnitDetail = Convert.ToString(rd["UnitDetail"].ToString()); ts.UnitActive = Convert.ToBoolean(rd["UnitActive"].ToString()); db.tb_Units.InsertOnSubmit(ts); db.SubmitChanges(); } else { x.UnitDetail = Convert.ToString(rd["UnitDetail"].ToString()); x.UnitActive = Convert.ToBoolean(rd["UnitActive"].ToString()); db.SubmitChanges(); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); dbClss.AddError("InportData", ex.Message, this.Name); } }
private bool AddUnit() { bool ck = false; int C = 0; try { radGridView1.EndEdit(); using (DataClasses1DataContext db = new DataClasses1DataContext()) { foreach (var g in radGridView1.Rows) { if (!Convert.ToString(g.Cells["UnitCode"].Value).Equals("") && g.IsVisible) { if (Convert.ToString(g.Cells["dgvC"].Value).Equals("T")) { if (Convert.ToString(g.Cells["dgvCodeTemp"].Value).Equals("")) { // MessageBox.Show("11"); tb_Unit u = new tb_Unit(); u.UnitCode = Convert.ToString(g.Cells["UnitCode"].Value); u.UnitActive = Convert.ToBoolean(g.Cells["UnitActive"].Value); u.UnitDetail = Convert.ToString(g.Cells["UnitDetail"].Value); db.tb_Units.InsertOnSubmit(u); db.SubmitChanges(); C += 1; dbClss.AddHistory(this.Name, "เพิ่ม", "Insert Unit Code [" + u.UnitCode + "]", ""); } else { var unit1 = (from ix in db.tb_Units where ix.UnitCode == Convert.ToString(g.Cells["dgvCodeTemp"].Value) select ix).First(); unit1.UnitDetail = Convert.ToString(g.Cells["UnitDetail"].Value); unit1.UnitActive = Convert.ToBoolean(g.Cells["UnitActive"].Value); C += 1; db.SubmitChanges(); dbClss.AddHistory(this.Name, "แก้ไข", "Update Unit Code [" + unit1.UnitCode + "]", ""); } } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); dbClss.AddError("AddUnit", ex.Message, this.Name); } if (C > 0) { MessageBox.Show("บันทึกสำเร็จ!"); } return(ck); }