ASP.NET Image Upload 'Object reference not set to an instance of an Object. DAL.Entities.Products.ImageFile.get returned null

1

I am almost new in asp.net MVC. This project is my college assignment.I need to create new Project DAL and connect it with first created project in the same Solution. After moving to model classes into DAL, and creating Repositoies, Image can't be uploaded. I can't find any solution related to my problem. My project without DAL works fine, I can upload image without any error. After adding DAL, there is error. Tried many times but no success. I am using EF Code First. I use FormMethod.Post in create view. Thank you in advance. Here my project on github https://github.com/AbdushukurRasulov/asp.netMVC

My Products Model

public class Products
{
    [Key]
    public int id { get; set; }
    public string ImagePath { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    [NotMapped]
    public HttpPostedFileBase ImageFile { get; set; }
}

This is my ProductRepository. So far I have only Get and Create Methods

public class ProductRepository : IRepository<Products>
{
    private ProductContext db = new ProductContext();

    public IQueryable<Products> GetAll()
    {
        return db.Products;
    }

    public Products GetById(int id)
    {
        return db.Products.SingleOrDefault(e => e.id == id);
    }

    public void Create(Products entity)
    {
            string fileName = Path.GetFileNameWithoutExtension(entity.ImageFile.FileName);
            string extension = Path.GetExtension(entity.ImageFile.FileName);
            entity.ImagePath = "~/Images/" + fileName;
            fileName = Path.Combine(HttpContext.Current.Server.MapPath("~/Image/"), fileName);
            entity.ImageFile.SaveAs(fileName);
            db.Products.Add(entity);
            db.SaveChanges();
    }

}

My Product Controller

// GET: Products/Create
    [HttpGet]
    public ActionResult Create()
    {
        return View();
    }
    // POST: Products/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "id, ImagePath, ProductName, Price, Description")] Products product)
    {
        if (ModelState.IsValid)
        {
            productRepo.Create(product);
            return RedirectToAction("Index");
        }
        return View(product);
    }

Error

  System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=DAL
  StackTrace:
   at DAL.Repositories.ProductRepository.Create(Products entity) in D:\WIUT\LEVEL 6BIS\WAD\WAD_CW2\CRUD_Image\DAL\Repositories\ProductRepository.cs:line 29
   at CRUD_Image.Controllers.ProductsController.Create(Products product) in D:\WIUT\LEVEL 6BIS\WAD\WAD_CW2\CRUD_Image\CRUD_Image\Controllers\ProductsController.cs:line 50
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__11_0()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
c#
asp.net
asp.net-mvc
asked on Stack Overflow Feb 12, 2020 by Abdushukur • edited Feb 12, 2020 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0