473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2329
zmbd
5,501 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

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

Similar topics

3
by: J. Muenchbourg | last post by:
The block of code below shows how I am inserting field values into my dbase table: strSQLStatement = "INSERT INTO tblArticles (handid,ArticleDate,sport,articleheader, fpick,articleText) "_ &...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
0
by: macupryk | last post by:
{System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProjectResponse__ProjectQuestionId". The conflict occurred in database "RG_ProjectData", table...
1
by: filip1150 | last post by:
I'm trying to find if there is any performance diference between explicitly using a sequence in the insert statement to generate values for a column and doing this in an insert trigger. I...
3
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
3
by: haiminnu | last post by:
I have created Two tables 1]EmployeeTable -------------------------- EmpID EmpName AccessLevelID ...
2
by: ksenthilbabu | last post by:
Hey All, I am using MSSQL -2005 with VB6. I have created a master table tblCompany and detail Table tblDetail having foreign key relationship. When i try to insert a value within a TRANSACTION I...
1
by: bougie | last post by:
i have two tables projects and proposals in project I have column ID and in proposal I have column ID too and there is a one to one realtionship between these tables the primary key in this realtion...
2
by: Good Guy | last post by:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Customers_Contact". The conflict occurred in database "BreakAway", table "dbo.Contact", column 'ContactID'. The statement has...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.