public static MilitaryUnit getEditedMilitaryUnit(MilitaryUnit m)
        {
            addMilitaryUnit wndM = new addMilitaryUnit(m);

            wndM.ShowDialog();
            return(wndM.mUnit);
        }
 private void btnOk_Click(object sender, RoutedEventArgs e)
 {
     if (txName.Text != "" && txShortName.Text != "" && txNumber.Text != "")
     {
         if (mUnit == null)
         {
             mUnit = new MilitaryUnit(
                 0,
                 txFullName.Text,
                 txName.Text,
                 txShortName.Text,
                 txNumber.Text,
                 txQuantity.Text == "" ? 0 : Int32.Parse(txQuantity.Text),
                 1);
         }
         else
         {
             mUnit.FullName  = txFullName.Text;
             mUnit.Name      = txName.Text;
             mUnit.ShortName = txShortName.Text;
             mUnit.Number    = txNumber.Text;
             mUnit.Quantity  = txQuantity.Text == "" ? 0 : Int32.Parse(txQuantity.Text);
         }
         this.Close();
     }
     else
     {
         MessageBox.Show("Не заполнены поля обязательные поля!");
     }
 }
Esempio n. 3
0
        internal static ObservableCollection <MilitaryUnit> getMilitaryUnitList(DataTable table)
        {
            ObservableCollection <MilitaryUnit> list = new ObservableCollection <MilitaryUnit>();

            if (table.Rows.Count > 0 && table.Columns.Count == 7)
            {
                try
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        MilitaryUnit r = new MilitaryUnit(
                            Int32.Parse(table.Rows[i][0].ToString()),
                            table.Rows[i][1].ToString(),
                            table.Rows[i][2].ToString(),
                            table.Rows[i][3].ToString(),
                            table.Rows[i][4].ToString(),
                            Int32.Parse(table.Rows[i][5].ToString() == "" ? "1" : table.Rows[i][5].ToString()),
                            Int32.Parse(table.Rows[i][6].ToString()));
                        list.Add(r);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Некорректные данные в БД:\n" + ex.Message);
                }
            }
            return(list);
        }
 internal void update(MilitaryUnit s)
 {
     for (int i = 0; i < values.Count; i++)
     {
         if (values[i].Id == s.Id)
         {
             values[i] = s;
         }
     }
 }
        public addMilitaryUnit(MilitaryUnit m)
        {
            InitializeComponent();

            txFullName.Focus();

            mUnit            = m;
            txFullName.Text  = m.FullName;
            txName.Text      = m.Name;
            txShortName.Text = m.ShortName;
            txNumber.Text    = m.Number;
            txQuantity.Text  = m.Quantity.ToString();
        }
        private void lbMilitaryUnit_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MilitaryUnit m = addMilitaryUnit.getEditedMilitaryUnit(lbMilitaryUnit.SelectedItem as MilitaryUnit);

            if (m != null)
            {
                if (sqlWorker.updateMilitaryUnit(m))
                {
                    mList.update(m);
                    lbMilitaryUnit.Items.Refresh();
                }
                else
                {
                    MessageBox.Show("Ошибка при изменении элемента");
                }
            }
        }
 internal bool updateMilitaryUnit(MilitaryUnit m)
 {
     try
     {
         executeQuery("UPDATE MilitaryUnit SET " +
                      "fullName = " + (m.FullName == "" ? "NULL" : "'" + m.FullName + "'") +
                      ", name = '" + m.Name +
                      "', shortName = '" + m.ShortName +
                      "', number = '" + m.Number +
                      "', quantity = " + (m.Quantity == 0 ? "NULL" : m.Quantity.ToString()) +
                      " WHERE idMilitaryUnit = " + m.Id + ";");
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        internal bool addMilitaryUnit(int id, MilitaryUnit newItem)
        {
            try
            {
                executeQuery("INSERT INTO MilitaryUnit VALUES(" +
                             id + ",'" +
                             newItem.FullName + "','" +
                             newItem.Name + "','" +
                             newItem.ShortName + "','" +
                             newItem.Number + "'," +
                             (newItem.Quantity == 0 ? "NULL" : newItem.Quantity.ToString()) + "," +
                             "1);");

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        private void btnAddMilitaryUnit_Click(object sender, RoutedEventArgs e)
        {
            MilitaryUnit m = addMilitaryUnit.getNewMilitaryUnit();

            if (m != null)
            {
                if (mList == null)
                {
                    mList = new MilitaryUnitList();
                }

                int id = sqlWorker.getNewId("MilitaryUnit");
                m.Id = id;
                if (sqlWorker.addMilitaryUnit(id, m))
                {
                    mList.values.Add(m);
                }
                else
                {
                    MessageBox.Show("Ошибка при добавлении элемента");
                }
            }
        }