private void button1_Click(object sender, EventArgs e) { try { DateTime date = DateTime.Now.Date; TimeSpan time = DateTime.Now.TimeOfDay; //dodac weryfikacje czy takie szkolenie jest juz w bazie(identyczne) if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0 && textBox3.Text.Length != 0 && textBox4.Text.Length != 0 && textBox4.Text.Length > 0 && textBox5.Text.Length != 0 && textBox5.Text.Length > 0 && (textBox4.Text.All(c => Char.IsNumber(c))) && (textBox5.Text.All(c => Char.IsNumber(c))) ) { if (MessageBox.Show("Are you sure you want to perform this operation?", "Question", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (db = new DataClasses1DataContext()) { trainings t = new trainings(); t.name = textBox1.Text; t.business = textBox2.Text; t.leader = textBox3.Text; t.price = Decimal.Parse(textBox4.Text); t.slot = Int32.Parse(textBox5.Text); t.description = textBox6.Text; t.start = Convert.ToDateTime(dateTimePicker1.Text); t.finish = Convert.ToDateTime(dateTimePicker2.Text); t.id_currency = Int32.Parse((comboBox1.SelectedValue.ToString())); t.active = true; t.date_add = date + time; db.trainings.InsertOnSubmit(t); db.SubmitChanges(); MessageBox.Show("New training added!"); } } } else { MessageBox.Show("Wrongly completed form. Please correct errors!"); } } catch (FormatException ex) { MessageBox.Show(ex.ToString()); } }
private void button3_Click(object sender, EventArgs e) { //UPDATE TRAINING try { if (MessageBox.Show("Are you sure you want to perform this operation?", "Question", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (db = new DataClasses1DataContext()) { var result = (from t in db.trainings where t.id_trainings == Decimal.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()) select t).Take(1); foreach (var t in result) { trainings trainings = db.trainings.Single(cp => cp.id_trainings == t.id_trainings); trainings.name = textBox1.Text; trainings.business = textBox2.Text; trainings.leader = textBox3.Text; trainings.price = Decimal.Parse(textBox4.Text); trainings.id_currency = Int32.Parse((comboBox1.SelectedValue.ToString())); trainings.slot = Int32.Parse(textBox5.Text); trainings.start = Convert.ToDateTime(dateTimePicker1.Text); trainings.finish = Convert.ToDateTime(dateTimePicker2.Text); trainings.description = textBox6.Text; trainings.active = checkBox1.Checked; db.SubmitChanges(); } //AKTUALIZACJA DATAGRID LIVE dataGridView1.DataSource = null; dataGridView1.Update(); dataGridView1.Refresh(); dataGridView1.DataSource = db.view_trainings; } MessageBox.Show("The data has been modified!"); } } catch (Exception ex) { MessageBox.Show("Error" + ex); } }