private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Validate()) return;
                var train = new tblTrain();
                train.Number = txtNumberTrain.Text.Trim();
                train.Type = (short)((ComboBoxItem)ddlTypeName.SelectedItem).Value;
                if (train.Type == (short)TrainType.TypeExport)
                {
                    train.IsExport = true;
                    train.DateExport = dtpRegisterDate.Value;
                }
                else if (train.Type == (short)TrainType.TypeImport)
                {
                    train.IsImport = true;
                    train.DateImport = dtpRegisterDate.Value;
                }
                train.NumberPartTrain = txtNumberPartofTrain.Text.Trim();

                train.CreatedBy = _userInfo.UserID;
                if (TrainFactory.Insert(train) > 0)
                {
                    MessageBox.Show("Thêm mới thành công!");
                    Reset();
                }
                else MessageBox.Show("Thêm mới không thành công!");

            }
            catch (Exception ex)
            {
                if (GlobalInfo.IsDebug) MessageBox.Show(ex.ToString());
            }
        }
        public static int Update(tblTrain train)
        {
            var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            train.ModifiedDate = CommonFactory.GetCurrentDate();
            var originTrain = _db.tblTrains.Where(g => g.TrainID == train.TrainID).FirstOrDefault();

            if (originTrain == null)
            {
                return -1;
            }
            _db.Attach(originTrain);
            _db.ApplyPropertyChanges("tblTrains", train);
            
            return _db.SaveChanges();
        }
 public static int Insert(tblTrain train)
 {
     var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     _db.Connection.Open();
     train.CreatedDate = CommonFactory.GetCurrentDate();
     train.BranchId= FDHelper.RgCodeOfUnit();
     _db.AddTotblTrains(train);
     try
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         return _db.SaveChanges();
     }
     catch (Exception ex)
     {
         return -1;
     }
     finally
     {
         _db.Connection.Close();
     }
 }
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Validate()) return;
                var train = new tblTrain();
                train.Number = txtNumberTrain.Text.Trim();
                var type = (short)((ComboBoxItem)ddlTypeName.SelectedItem).Value;
                train.Type = type;
                if (type == (short)TrainType.TypeExport || type == (short)TrainType.TypeExportChange || type == (short)TrainType.TypeExportNormal)
                {
                    train.IsExport = true;
                    train.DateExport = CommonFactory.GetCurrentDate();
                }
                else
                {
                    train.IsImport = true;
                    train.DateImport = CommonFactory.GetCurrentDate();
                }
                if (type == (short)TrainType.TypeExportChange || type == (short)TrainType.TypeImportChange)
                {
                    train.NumberHandover = txtNumberHandover.Text.Trim();
                    train.DateHandover = dtpHandover.Value;
                    train.CodeStation = txtCodeCuaKhau.Text.Trim();
                    train.CodeStationFromTo = txtCodeGaDenDi.Text.Trim();
                    train.StatusVehicle = txtStatusVehicle.Text.Trim();
                    train.StatusGoods = txtStatusGood.Text.Trim();
                }

                train.CreatedBy = _userInfo.UserID;
                if (TrainFactory.Insert(train) > 0)
                {
                    //insert tờ khai
                    if (type == (short)TrainType.TypeExportNormal || type == (short)TrainType.TypeImportNormal)
                    {
                        var declare = new tblToKhaiTau()
                                          {
                                              Number = Convert.ToInt32(txtExportNumber.Text.Trim()),
                                              DateDeclaration = dtpDeclaration.Value,
                                              TypeCode = txtTypeExport.Text.Trim(),
                                              CustomCode = txtCustomsCode.Text.Trim(),
                                              CreatedBy = _userInfo.UserID,
                                              TrainID = train.TrainID
                                          };
                        TrainFactory.InsertToKhai(declare);

                    }
                    //insert toa tàu
                    if (grdToaTau.Rows.Count > 0)
                    {
                        var listToaTau = new List<tblToaTau>();
                        for (int i = 0; i < grdToaTau.Rows.Count; i++)
                        {
                            var toaTau = (tblToaTau) grdToaTau.Rows[i].DataBoundItem;
                            toaTau.CreatedBy = _userInfo.UserID;
                            toaTau.TrainID = train.TrainID;
                            listToaTau.Add(toaTau);
                        }
                        TrainFactory.InsertToaTau(listToaTau);
                    }

                    MessageBox.Show("Thêm mới thành công!");
                    Reset();
                }
                else MessageBox.Show("Thêm mới không thành công!");
            }
            catch (Exception ex)
            {
                if (GlobalInfo.IsDebug) MessageBox.Show(ex.ToString());
            }
        }