Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:10:54+00:00 2026-06-10T18:10:54+00:00

I have the following code where I am selecting a file. But no matter

  • 0

I have the following code where I am selecting a file. But no matter what file I select I get the following exception:

Cannot insert the value NULL into column ImgPath.

It seems like file is not passing any value back to controller, am I doing something wrong?

create.cshtml:

<div class="editor-label">
    @Html.LabelFor(model => model.ImgPath)
</div>
<div class="editor-field">
    <input type="file" name="file" />
</div>

Home Controller:

public ActionResult Create(TopSong topsong, HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        //verify user has selected file
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath(
                "~/Content/themes/base/images/Movie Images"), fileName);

            file.SaveAs(path);
        }
        db.TopSongs.Add(topsong);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    ViewBag.DateId = new SelectList(db.TopDates, "DateId", "DateId", 
                                    topsong.DateId);

    return View(topsong);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:10:55+00:00Added an answer on June 10, 2026 at 6:10 pm

    You are never setting the ImgPath property on your top song object. You have a label for it in your view, but that does not assign value. If I read your code correctly, you are needing to store the image path in the database along with some other stuff, so you should just need to add one line:

    topsong.ImgPath = path;
    

    But as your code stands now, it looks like you are always going to be saving a top song, even if the user does not upload an image (your saving is outside of the if statement). By refactoring your function and reducing nesting, this should do what you want it to do

    public ActionResult Create(TopSong topsong, HttpPostedFileBase file)
    {
        if (!ModelState.IsValid)
        {
           ViewBag.DateId = new SelectList(db.TopDates, "DateId", "DateId",                                  topsong.DateId);
             Return this.View(topsong);
        }
    
    //verify user has selected file
        if (file == null || file.ContentLength == 0)
        {
             ViewBag.DateId = new SelectList(db.TopDates, "DateId", "DateId",                                  topsong.DateId);
             ModelState.AddModelError("file", "You must choose a file to upload.");
             Return this.View(topsong);
        }
    
        var fileName = Path.GetFileName(file.FileName);
        topsong.ImgPath = Path.Combine(Server.MapPath("~/Content/themes/base/images/Movie Images"), fileName);
        file.SaveAs(topsong.ImgPath);
        db.TopSongs.Add(topsong);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have following code for inserting data into database using PDO. It inserts data
I have following code using hibernate to throw a custom exception on error and
I have the following code which should make drawFrame be called every frame but
I have the following code which loads a xml file to populate a dropdown.
I have the following code, which I want to load values on a selection
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.