private void cmdDeleteAd_Click(object sender, EventArgs e) { Advert selectedad = (Advert)(listboxmyad.SelectedItem); using (AnnonserEntities1 db = new AnnonserEntities1()) { //Här görs databasanrop dvs en command med en sql fråga som läser ut just denna leverantören Advert advert = db.Adverts.Where(s => s.AdID == selectedad.AdID).FirstOrDefault(); db.Adverts.Remove(advert); db.SaveChanges(); MessageBox.Show("Annons borttagen"); } }
private void cmdCreateNew_Click(object sender, EventArgs e) { using (AnnonserEntities1 db = new AnnonserEntities1()) { Advert advert = new Advert(); advert.Title = txtAdTitel.Text; advert.Description = txtAdInfo.Text; advert.Price = Convert.ToInt32(txtPrice.Text); advert.Location = txtLocation.Text; advert.AdvertDate = DateTime.Now; advert.CategoryID = cboCategory.SelectedIndex; advert.UserID = this.UserID; db.Adverts.Add(advert); db.SaveChanges(); MessageBox.Show("Annons sparad"); } }
private void cmdUpdateAd_Click(object sender, EventArgs e) { Advert selectedad = (Advert)(listboxmyad.SelectedItem); using (AnnonserEntities1 db = new AnnonserEntities1()) { //Här görs databasanrop dvs en command med en sql fråga som läser ut just denna leverantören Advert advert = db.Adverts.Where(s => s.AdID == selectedad.AdID).FirstOrDefault(); advert.Title = txtAdTitel.Text; advert.Description = txtAdInfo.Text; advert.Price = Convert.ToInt32(txtPrice.Text); advert.Location = txtLocation.Text; advert.AdvertDate = DateTime.Now; advert.CategoryID = cboCategory.SelectedIndex; db.SaveChanges(); MessageBox.Show("Annons uppdaterad"); } }