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 button2_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show(this, "Are you sure you want to delete accout? It will delete all the associated data !!", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Stop); if (result == DialogResult.Yes) { using (var context = new ZakatCS()) { using (var transaction = context.Database.BeginTransaction()) { try { var account = acc.GetAll().Where(a => a.AID == new Guid(label1.Text)).FirstOrDefault(); var years = context.ZakatYears.Where(b => b.AID == account.AID).ToList(); foreach (var item in years) { context.ZakatItems.RemoveRange(context.ZakatItems.Where(c => c.ZakatYear.ZYear == item.ZYear)); } foreach (var item in years) { context.ZakatYears.RemoveRange(context.ZakatYears.Where(d => d.AID == account.AID)); } // context.SaveChanges(); acc.Delete(account); transaction.Commit(); if (_form1 != null) { Form1 temp = _form1; temp.Close(); } } catch (Exception ex) { transaction.Rollback(); MessageBox.Show(ex.Message); } } } } else { label1.Text = "Bach gya saley"; } }
private void button2_Click(object sender, EventArgs e) { using (var context = new ZakatCS()) { using (var transaction = context.Database.BeginTransaction()) { try { var year = previousyears.GetAll().Where(b => b.AID == Form1.guid) .AsEnumerable().FirstOrDefault(c => c.ZYear == textBox1.Text); context.ZakatItems.RemoveRange(context.ZakatItems.Where(a => a.ZakatYear.ZYear == year.ZYear)); previousyears.Delete(year); transaction.Commit(); MessageBox.Show("Year and Associated Data Deleted"); } catch (Exception ex) { transaction.Rollback(); MessageBox.Show(ex.Message); } } } }
int Zakatgrid(Guid guid, string year) { ZakatCS zakatCS = new ZakatCS(); //The entity or complex type cannot be constructed in a LINQ to Entities query. // the above error was removed with the help of //https://social.msdn.microsoft.com/Forums/en-US/cdd1e6b3-11e3-4341-ae90-9f6093010c0c/the-entity-or-complex-type-categories-cannot-be-constructed-in-a-linq-to-entities-query?forum=adodotnetentityframework var newlist = (from a in zakatCS.Accounts join b in zakatCS.ZakatYears on a.AID equals b.AID join c in zakatCS.ZakatItems on b.ZID equals c.ZID where a.AID == guid && b.ZYear == year select(new { ID = c.ID, ItemName = c.ItemName, Description = c.Description, MarketPrice = c.MarketPrice, ItemZakat = c.ItemZakat, year = c.ZakatYear })).ToList().Select(item => new ZakatItem { ID = item.ID, Description = item.Description, ItemName = item.ItemName, MarketPrice = item.MarketPrice, ItemZakat = item.ItemZakat, }).ToList(); //var list = ZakatItemView.Find(a => a.ZakatYear.ZYear == label3.Text).ToList(); var bindinglist = new BindingList <ZakatItem>(newlist); dataGridView1.DataSource = newlist; return(newlist.Count); }