public void AddCategory(Categories Catag) { int CatId = 1; if (_Context.Categories.Count() > 0) { CatId = (_Context.Categories.Select(x => x.CategoryId).Max() + 1); } Catag.CategoryId = CatId; _Context.Categories.Add(Catag); _Context.SaveChanges(); }
public async Task <IActionResult> Create([Bind("BrandId,BrandName,BrandDescription,BrandLogoPicturePath")] Brands brands, IFormFile _Imag) { if (ModelState.IsValid) { brands.BrandId = GetMaxBrandNo(); _context.Add(brands); await _context.SaveChangesAsync(); SaveBrand_Image(brands, _Imag); _context.SaveChanges(); return(RedirectToAction(nameof(Index))); } return(View(brands)); }
void SaveCatg_Image(Categories _Catg, IFormFile _Imag) { try { string ImagePath = string.Empty; string FileExtension = ""; if (_Imag != null) { if (_Imag.Length > 0) { FileExtension = Path.GetExtension(_Imag.FileName); ImagePath = Directory.GetCurrentDirectory(); ImagePath = Path.Combine(ImagePath, "wwwroot", "Images", "CatagoriesPic", $"{_Catg.CategoryId}{FileExtension}"); FileStream fs = new FileStream(ImagePath, FileMode.Create); _Imag.CopyTo(fs); fs.Close(); } _Catg.CategoryPicturePath = $"{_Catg.CategoryId}{FileExtension}"; } else { _Catg.CategoryPicturePath = ""; } } catch (Exception ex) { int lineNumber = (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber(); ErrorTb err = new ErrorTb() { ErrDate = DateTime.Now, ErrId = (_context.ErrorTb.Select(x => x.ErrId).Max()) + 1, ErrMessage = ex.GetType().Name, ErrLine = lineNumber, }; _context.ErrorTb.Add(err); _context.SaveChanges(); } }
public void AddProduct(Products _Product, MobileProperties Mobile, LaptopProperties Laptop) { _Product.ProductId = (_Context.Products.Select(x => x.ProductId).Max() + 1); _Context.Products.Add(_Product); _Context.SaveChanges(); if (Mobile != null) { Mobile.ProductId = _Product.ProductId; _Context.MobileProperties.Add(Mobile); _Context.SaveChanges(); } if (Laptop != null) { Laptop.ProductId = _Product.ProductId; _Context.LaptopProperties.Add(Laptop); _Context.SaveChanges(); } }