private void button1_Click(object sender, EventArgs e) { using (var context = new ZakatCS()) { using (var transaction = context.Database.BeginTransaction()) { IList <ZakatItem> items = new List <ZakatItem>(); IRepository <ZakatItem> zakatitems = new Implementation <ZakatItem>(); try { var fromzid = previousyears.FirstOrDefault(a => a.ZYear == FromcomboBox.Text && a.AID == new Guid(guidlabel.Text)).ZID; var currentzid = previousyears.FirstOrDefault(a => a.ZYear == TocomboBox.Text && a.AID == new Guid(guidlabel.Text)).ZID; IEnumerable <ZakatItem> fromitems = zakatitems.GetAll().Where(a => a.ZID == fromzid); foreach (var item in fromitems) { ZakatItem toitem = new ZakatItem { ItemName = item.ItemName, ItemZakat = item.ItemZakat, Description = item.Description, MarketPrice = item.MarketPrice, ZID = currentzid }; items.Add(toitem); //context.ZakatItems.Add(toitem); //zakatitems.Insert(toitem); } // some unknown reasons, addrange() is not working.. // its working now, we were not saving changes. context.ZakatItems.AddRange(items); context.SaveChanges(); //.Where(a => a.ZakatYear.ZYear == TocomboBox.Text)); transaction.Commit(); MessageBox.Show($"Data Copied from {FromcomboBox.Text} to {TocomboBox.Text}"); } catch (Exception ex) { transaction.Rollback(); MessageBox.Show(ex.Message); } } } }
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == string.Empty) { MessageBox.Show("Cannot Leave Name Empty"); } if (textBox3.Text == string.Empty) { MessageBox.Show("Cannot Leave Market Price Empty"); } if (!double.TryParse(textBox3.Text, out double a)) { MessageBox.Show("Please Insert Numerical Value"); textBox3.Focus(); } using (ZakatItem newitem = new ZakatItem()) { newitem.ItemName = textBox1.Text; newitem.Description = textBox2.Text; newitem.MarketPrice = markettprice; newitem.ItemZakat = itemzakat; // in below vaiable, guid is get to insert value in the year of correct user int zakatYear = allyears.FirstOrDefault(year => year.ZYear == label3.Text && year.AID == new Guid(lblGUID.Text)).ZID; newitem.ZID = zakatYear; try { ZakatItemView.Insert(newitem); MessageBox.Show("New Item Added"); var count = Zakatgrid(new Guid(lblGUID.Text), label3.Text); label6.Text = calculatezakat(dataGridView1, count); label7.Text = count.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace + "\n" + ex.Source + "\n" + ex.ToString()); } } }