468,114 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,114 developers. It's quick & easy.

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint

2
I have a problem and I don't know why am I getting this bug.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE Course(
  2.     IdCourse int IDENTITY (1,1) NOT NULL PRIMARY KEY,
  3.     NameCourse nvarchar(50) NOT NULL,
  4.     Description nvarchar(250) NOT NULL,
  5.     Date datetime NOT NULL,
  6.     Full BIT NULL DEFAULT 0
  7. )
  8.  
  9. CREATE TABLE Application
  10. (
  11.     IdApplication int IDENTITY (1,1) NOT NULL PRIMARY KEY,
  12.     Date datetime NOT NULL,
  13.     Name nvarchar(25) NOT NULL,
  14.     Surname nvarchar(25) NOT NULL,
  15.     Adress nvarchar(50) NOT NULL,
  16.     Email nvarchar(320) NOT NULL,
  17.     Telephone varchar(15) NOT NULL,
  18.     IdCourse int FOREIGN KEY REFERENCES Course(IdCourse),
  19.     Status INT not nulL
  20. )
  21.  
  22.  
  23. public ActionResult Application(int id)
  24.         {
  25.             ViewBag.Course = (
  26.                 from c in db.Course
  27.                 where c.IdCourse == id
  28.                 select s.NameCourse).FirstOrDefault();
  29.             Application application = new Application();
  30.             return View(application);
  31.         }
  32.  
  33. [HttpPost]
  34.         [ValidateAntiForgeryToken]
  35.         public ActionResult Application([Bind(Include = "IdApplication,Name,Surname,Adress,Email,Telephone")] Application application)
  36.         {
  37.             Course course = new course();
  38.             application.IdCourse = course.IdCourse;
  39.             int applicationId = application.IdApplication;
  40.  
  41.             if (ModelState.IsValid)
  42.             {
  43.                 db.Applications.Add(Application);
  44.                 db.SaveChanges();
  45.                 return RedirectToAction("Index");
  46.             }
  47.  
  48.             return View(application);
  49.         }
  50.  
My view
Expand|Select|Wrap|Line Numbers
  1. @model Aplikacija.Models.Applicaton
  2.  
  3. @{
  4.     <strong>@ViewBag.Course</strong>
  5. }
  6.  
  7.  
  8. @using (Html.BeginForm())
  9. {
  10.     @Html.AntiForgeryToken()
  11.  
  12.     <div class="form-horizontal"> <hr />
  13.         @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  14.         @Html.HiddenFor(model => model.IdApplication)
  15.  
  16.         <div class="form-group">
  17.             @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
  18.             <div class="col-md-10">
  19.                 @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
  20.                 @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
  21.             </div> </div> <div class="form-group">
  22.             @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
  23.             <div class="col-md-10">
  24.                 @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
  25.                 @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
  26.             </div> </div> <div class="form-group">
  27.             @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
  28.             <div class="col-md-10">
  29.                 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
  30.                 @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
  31.             </div> </div> <div class="form-group">
  32.             @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
  33.             <div class="col-md-10">
  34.                 @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
  35.                 @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
  36.             </div> </div> <div class="form-group">
  37.             @Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label col-md-2" })
  38.             <div class="col-md-10">
  39.                 @Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
  40.                 @Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
  41.             </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-primary" /> </div> </div> </div>
  42. }
  43.  
  44. <div>
  45.     @Html.ActionLink("Back to List", "Index")
  46. </div>
  47.  
  48. @section Scripts {
  49.     @Scripts.Render("~/bundles/jqueryval")
  50. }
Jun 8 '19 #1
2 1791
zmbd
5,400 Expert Mod 4TB
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint
Is that the full error message?

Typically, this would indicate that you have a value in the column for the foreign key reference to another table that is not also in the primary key column of the referenced table.

Without the full error message that's about as far as we can offer...
Jun 9 '19 #2
ezra89
2
I changed my HttpPost and there is no exception anymore. It just gives me new id of the Course table and I don't want that. I need to place it in the row that I chose in ViewBag from GET.

Expand|Select|Wrap|Line Numbers
  1. [HttpPost]
  2.         [ValidateAntiForgeryToken]
  3.         public ActionResult Application(Application application)
  4.         {
  5.             application.Course = new Course();
  6.             if (ModelState.IsValid)
  7.             {
  8.                 db.Applications.Add(application);
  9.                 db.SaveChanges();
  10.                 return RedirectToAction("Index");
  11.             }
  12.  
  13.             ViewBag.Course = new SelectList(db.Courses, "IdCourse", "NameCourse", application.IdCourse);
  14.             return View(application);
  15.         }
Thank you for your reply.
Jun 10 '19 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Geoffrey KRETZ | last post: by
3 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.