0

I've implemented simple create method to put data in database, and it worked fine. I've decided to add image upload, but I keep getting NullReferenceException (file is null) and I can't figure out why. I hope you can help me!

Here's my code:

[Authorize]
    public ActionResult Create()
    {
        ViewBag.CategoryId = new SelectList(_categoryRepository.GetAllCategories().AsEnumerable(), "CategoryId",
                                            "Name");
        return View();
    }

    //
    // POST: /Advert/Create

    [HttpPost, Authorize]
    public ActionResult Create(CompetitionDTO competitionDTO, HttpPostedFileBase file)
    {
        if (file.ContentLength > 0)
        {
            string fileName = Path.GetFileName(file.FileName);
            string fileExtension = Path.GetExtension(fileName);
            if ((fileExtension == ".jpg") || (fileExtension == ".png"))
            {
                string path = Path.Combine(Server.MapPath("~/Uploads/Images"), fileName);
                file.SaveAs(path);
                competitionDTO.ImageURL = path;
            }
        }
        if (ModelState.IsValid)
        {
            _competitionRepository.AddCompetition(competitionDTO, WebSecurity.CurrentUserId);


            return RedirectToAction("Index");
        }

        ViewBag.CompetitionId = new SelectList(_competitionRepository.GetAllCompetitions().AsEnumerable(),
                                               "CompetitionId", "Name");


        return View(competitionDTO);
    }
}

and View

<div>
        @using (Html.BeginForm("Create", "Competition", FormMethod.Post, new
            {
                enctype = "multipart/form-data"
                , id = "parentForm"
            }))
        {
            <input type="file" name="file" id="file"/>
            <input type="submit" value="Create" />
        }
    </div>

EDIT

I if I wasn't clear enough:

I know how to insert data to database and I know how to upload file, and I want when I click submit that those 2 things happen at same time

hyperN
  • 2,674
  • 9
  • 54
  • 92
  • Are you using IE? If yes, tried another browser? – Max Mar 20 '13 at 23:17
  • Which line of code specifically is generating the exception? – Erik Funkenbusch Mar 20 '13 at 23:19
  • @Max that is irrelevant. OP, at what line does the exception occur? – CodeCaster Mar 20 '13 at 23:20
  • I'm using Chrome, and on line if (file.ContentLength > 0) – hyperN Mar 20 '13 at 23:23
  • I've noticed smth. when I surround first if statement with `if(file != null)` I got eception in view about dropdownlist and when I removed HttpPostedFileBase file, everything worked fine again – hyperN Mar 20 '13 at 23:26
  • The exception is `The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable'.`, and code for that part of view is `@Html.DropDownList("CategoryId")` – hyperN Mar 20 '13 at 23:28
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Mar 20 '13 at 23:30
  • Well I know why I'm getting `NullReferenceException` it's because file is `null`, and I can't figure out why it is `null` – hyperN Mar 20 '13 at 23:32
  • Check `Request.Files["file"]` whether you have file data or not – Karthik Chintala Mar 21 '13 at 04:11
  • I don't have file data and I don't know why I'm not getting it – hyperN Mar 21 '13 at 08:55

1 Answers1

0

It was my mistake I had two Html.BeginForms, now I merged them into one and it works fine

hyperN
  • 2,674
  • 9
  • 54
  • 92