//Обновление строки
        private void UpdateRecord()
        {
            if (dgvRefSocBenefit.CurrentRow == null)
            {
                return;
            }
            string        error;
            RefSocBenefit refSocBenefit = dgvRefSocBenefit.CurrentRow.DataBoundItem as RefSocBenefit;

            if (refSocBenefit == null)
            {
                MessageBox.Show("Не знайдений рядок для оновлення", "Помилка");
                return;
            }
            fmRefSocBenefitEdit fmEdit = new fmRefSocBenefitEdit(EnumFormMode.Edit, "Зміна інтервалу соціальної пільги");

            fmEdit.AddControlPeriod(socBenefits);
            fmEdit.SetData(refSocBenefit);
            if (fmEdit.ShowDialog() == DialogResult.OK)
            {
                refSocBenefit = fmEdit.GetData();
                if (!_repository.ModifyRefSocBenefit(refSocBenefit, out error))
                {
                    MessageBox.Show("Помилка оновлення рядка.\nТехнічна інформація: " + error, "Помилка");
                    return;
                }
                RefreshTable();
            }
        }
        //Физическое удаление строки
        private void DeleteRecord()
        {
            if (dgvRefSocBenefit.CurrentRow == null)
            {
                return;
            }
            if (MessageBox.Show("Ви впевнені, що бажаєте видалити рядок?", "Видалення", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            RefSocBenefit refSocBenefit = dgvRefSocBenefit.CurrentRow.DataBoundItem as RefSocBenefit;

            if (refSocBenefit == null)
            {
                MessageBox.Show("Не знайдений рядок для видалення", "Помилка");
                return;
            }
            string error;

            if (!_repository.DeleteRefSocBenefit(refSocBenefit.RefSocBenefit_Id, out error))
            {
                MessageBox.Show("Помилка видалення рядка.\nТехнічна інформація: " + error, "Помилка");
                return;
            }
            RefreshTable();
        }
        private void FillDataRec(SqlDataReader reader, RefSocBenefit RefSocBenefit)
        {
            int      resInt  = 0;
            DateTime resDate = DateTime.MinValue;
            decimal  resDec  = 0;

            if (int.TryParse(reader["RefSocBenefit_Id"].ToString(), out resInt))
            {
                RefSocBenefit.RefSocBenefit_Id = resInt;
            }
            if (DateTime.TryParse(reader["RefSocBenefit_PerBeg"].ToString(), out resDate))
            {
                RefSocBenefit.RefSocBenefit_PerBeg = resDate;
            }
            if (DateTime.TryParse(reader["RefSocBenefit_PerEnd"].ToString(), out resDate))
            {
                RefSocBenefit.RefSocBenefit_PerEnd = resDate;
            }
            if (decimal.TryParse(reader["RefSocBenefit_Sm"].ToString(), out resDec))
            {
                RefSocBenefit.RefSocBenefit_Sm = resDec;
            }
            if (decimal.TryParse(reader["RefSocBenefit_LimSm"].ToString(), out resDec))
            {
                RefSocBenefit.RefSocBenefit_LimSm = resDec;
            }
        }
        //Изменить интервал прожиточного минимума
        public bool ModifyRefSocBenefit(RefSocBenefit refSocBenefit, out string error)
        {
            error = string.Empty;
            if (conn == null)
            {
                error = "conn == null";
                return(false);
            }
            if (refSocBenefit == null)
            {
                error = "refSocBenefit == null";
                return(false);
            }
            SqlCommand command = new SqlCommand(spRefSocBenefitUpdate, conn);

            command.CommandType = CommandType.StoredProcedure;
            command.Connection  = conn;
            command.Parameters.AddWithValue("@inRefSocBenefit_Id", refSocBenefit.RefSocBenefit_Id);
            command.Parameters.AddWithValue("@inRefSocBenefit_PerBeg",
                                            refSocBenefit.RefSocBenefit_PerBeg == DateTime.MinValue ? Convert.DBNull : refSocBenefit.RefSocBenefit_PerBeg);
            command.Parameters.AddWithValue("@inRefSocBenefit_PerEnd",
                                            refSocBenefit.RefSocBenefit_PerEnd == DateTime.MinValue ? Convert.DBNull : refSocBenefit.RefSocBenefit_PerEnd);
            command.Parameters.AddWithValue("@inRefSocBenefit_Sm", refSocBenefit.RefSocBenefit_Sm);
            command.Parameters.AddWithValue("@inRefSocBenefit_LimSm", refSocBenefit.RefSocBenefit_LimSm);
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(false);
            }
            return(true);
        }
        public RefSocBenefit GetData()
        {
            RefSocBenefit socBenefit = new RefSocBenefit();

            socBenefit.RefSocBenefit_Id = _id;

            DateTime perBeg;

            DateTime.TryParse(tbPerBeg.Text, out perBeg);
            socBenefit.RefSocBenefit_PerBeg = perBeg;

            DateTime perEnd;

            DateTime.TryParse(tbPerEnd.Text, out perEnd);
            socBenefit.RefSocBenefit_PerEnd = perEnd;

            decimal sm = 0;

            decimal.TryParse(tbSm.Text, out sm);
            socBenefit.RefSocBenefit_Sm = sm;

            decimal.TryParse(tbLimSm.Text, out sm);
            socBenefit.RefSocBenefit_LimSm = sm;
            return(socBenefit);
        }
 public void SetData(RefSocBenefit socBenefit)
 {
     _id = socBenefit.RefSocBenefit_Id;
     if (socBenefit.RefSocBenefit_PerBeg != DateTime.MinValue)
     {
         tbPerBeg.Text = socBenefit.RefSocBenefit_PerBeg.ToShortDateString();
     }
     if (socBenefit.RefSocBenefit_PerEnd != DateTime.MinValue)
     {
         tbPerEnd.Text = socBenefit.RefSocBenefit_PerEnd.ToShortDateString();
     }
     tbSm.Text    = socBenefit.RefSocBenefit_Sm.ToString();
     tbLimSm.Text = socBenefit.RefSocBenefit_LimSm.ToString();
 }
        //Добавить интервал социальной льготы
        public int AddRefSocBenefit(RefSocBenefit refSocBenefit, out string error)
        {
            error = string.Empty;
            if (refSocBenefit == null)
            {
                error = "refSocBenefit == null";
                return(0);
            }
            if (conn == null)
            {
                error = "conn == null";
                return(0);
            }
            SqlCommand command = new SqlCommand(spRefSocBenefitInsert, conn);

            command.CommandType = CommandType.StoredProcedure;
            command.Connection  = conn;
            command.Parameters.AddWithValue("@inRefSocBenefit_PerBeg",
                                            refSocBenefit.RefSocBenefit_PerBeg == DateTime.MinValue ? Convert.DBNull : refSocBenefit.RefSocBenefit_PerBeg);
            command.Parameters.AddWithValue("@inRefSocBenefit_PerEnd",
                                            refSocBenefit.RefSocBenefit_PerEnd == DateTime.MinValue ? Convert.DBNull : refSocBenefit.RefSocBenefit_PerEnd);
            command.Parameters.AddWithValue("@inRefSocBenefit_Sm", refSocBenefit.RefSocBenefit_Sm);
            command.Parameters.AddWithValue("@inRefSocBenefit_LimSm", refSocBenefit.RefSocBenefit_LimSm);
            // определяем выходной параметр
            SqlParameter outId = new SqlParameter
            {
                ParameterName = "outId",
                Direction     = ParameterDirection.Output,
                SqlDbType     = SqlDbType.Int
            };

            command.Parameters.Add(outId);
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(0);
            }
            int id = 0;

            int.TryParse(command.Parameters["outId"].Value.ToString(), out id);
            return(id);
        }
        private List <RefSocBenefit> socBenefits    = null; //Кеширование
        //Вставка строки
        private void InsertRecord()
        {
            fmRefSocBenefitEdit fmEdit = new fmRefSocBenefitEdit(EnumFormMode.Insert, "Створення інтервалу соціальної пільги");

            fmEdit.AddControlPeriod(socBenefits);
            if (fmEdit.ShowDialog() == DialogResult.OK)
            {
                string        error;
                RefSocBenefit socBenefits = fmEdit.GetData();
                int           id          = _repository.AddRefSocBenefit(socBenefits, out error);
                if (id == 0)
                {
                    MessageBox.Show("Помилка додавання рядка.\nТехнічна інформація: " + error, "Помилка");
                    return;
                }
                RefreshTable();
                dgvRefSocBenefit.SetPositionRow <RefSocBenefit>("RefSocBenefit_Id", id.ToString());
            }
        }
        //Получить интервалы социальной льготы
        public List <RefSocBenefit> GetAllRefSocBenefits(out string error)
        {
            error = string.Empty;

            List <RefSocBenefit> refSocBenefits = new List <RefSocBenefit>();

            if (conn == null)
            {
                error = "conn == null";
                return(refSocBenefits);
            }

            SqlCommand command = new SqlCommand(spRefSocBenefitSelect, conn);

            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = null;

            try
            {
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    RefSocBenefit refMinSalary = new RefSocBenefit();
                    FillDataRec(reader, refMinSalary);
                    refSocBenefits.Add(refMinSalary);
                }
            }
            catch (Exception exc)
            {
                error = exc.Message;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return(refSocBenefits);
        }