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 3335714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:03:00+00:00 2026-05-18T00:03:00+00:00

I knew this was a problem I would run into and I’m just not

  • 0

I knew this was a problem I would run into and I’m just not sure how to solve it. I have two tables with the following structures:

dbo.File
FileID            int
Title             nvarchar(50)
ISBN              nvarchar(50)
UploadDate        datetime
UserName          nvarchar(50)

dbo.Cover
CoverID           int
CoverFileContent  varbinary(max)
CoverMimeType     nvarchar(50)
CoverFileName     nvarchar(50)
FileID            int

The file table has a one to many relationship with the cover table, and FileID in the Cover table references FileID in the File table.

So I also have an MVC application. In this application, on one screen I insert the File Data, then in the following screen I upload a file, and insert this into the cover table, however I get the following error:

InnerException {“The INSERT statement
conflicted with the FOREIGN KEY
constraint \”FK_Cover_File\”. The
conflict occurred in database
\”SampleAppDB\”, table \”dbo.File\”,
column ‘FileID’.\r\nThe statement has
been terminated.”} System.Exception
{System.Data.SqlClient.SqlException}

I know what it means, I’ve just gone blank as to how I actually perform this insert statement to correspond with the File table. Currently, I’m not inserting anything into the FileID foreign key in the Cover table because I’m not sure how to go about it. Here is a snippet from the Cover controller where the upload of the cover file occurs:

[HttpPost]
public ActionResult CreateCover(FormCollection formvalues)
{
    Cover cover = new Cover();

    cover.CoverMimeType = Request.Files["CoverUpload"].ContentType;
    Stream fileStream = Request.Files["CoverUpload"].InputStream;
    cover.CoverFileName = Path.GetFileName(Request.Files["CoverUpload"].FileName);
    int fileLength = Request.Files["CoverUpload"].ContentLength;
    cover.CoverFileContent = new byte[fileLength];
    fileStream.Read(cover.CoverFileContent, 0, fileLength);

    filerepository.AddCoverData(cover);

    filerepository.Save();

    return View(cover);
}

any advice on how to handle this problem would be greatly appreciated, if anyone needs me to post more info too, just ask.

As requested: filerepository.cs

private SampleAppDBEntities entities = new SampleAppDBEntities();

        public void AddCoverData(Cover cover)
        {
            entities.Covers.AddObject(cover);
        }
  • 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-05-18T00:03:00+00:00Added an answer on May 18, 2026 at 12:03 am

    You could have the FileID passed within your FormCollection values as an hidden param
    and then have a Cover controller action that initialize it.

    For example:

    From the File detail page add a link to create a Cover

    <a href="#" rel='<%= Url.Content("~/Cover/AddCover/FILEID_HERE") %>'>Add cover</a>
    

    then you can have a get action on the Cover controller that simply initialize the cover FileID and return the AddCover view

    [HttpGet]
    public ActionResult AddCover(int id) {
        CoverModel cm = new CoverModel();
        cm.FileId = id;
        return View("AddCover", cm);
    }
    

    Finally within your Cover View, that should be typed to the CoverModel simply add the FileID as an hidden field so you can have it posted with the rest of your form data.

    <% using (Html.BeginForm("AddCover", "Cover", FormMethod.Post, new {} ))  { %>
        <%= Html.HiddenFor(model => model.FileID) %>
    
        //Place your cover form fields here
    
    <% } %>
    

    One effect of using this pattern is that you can take advantage of using the default model binder features of MVC and have a post action like this one instead of using the FormCollection class:

    [HttpPost]
    public ActionResult AddCover( CoverModel model ) {
       //Validate your data and tehn add to database
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.