I have a problem and I don't know why am I getting this bug. - CREATE TABLE Course(
-
IdCourse int IDENTITY (1,1) NOT NULL PRIMARY KEY,
-
NameCourse nvarchar(50) NOT NULL,
-
Description nvarchar(250) NOT NULL,
-
Date datetime NOT NULL,
-
Full BIT NULL DEFAULT 0
-
)
-
-
CREATE TABLE Application
-
(
-
IdApplication int IDENTITY (1,1) NOT NULL PRIMARY KEY,
-
Date datetime NOT NULL,
-
Name nvarchar(25) NOT NULL,
-
Surname nvarchar(25) NOT NULL,
-
Adress nvarchar(50) NOT NULL,
-
Email nvarchar(320) NOT NULL,
-
Telephone varchar(15) NOT NULL,
-
IdCourse int FOREIGN KEY REFERENCES Course(IdCourse),
-
Status INT not nulL
-
)
-
-
-
public ActionResult Application(int id)
-
{
-
ViewBag.Course = (
-
from c in db.Course
-
where c.IdCourse == id
-
select s.NameCourse).FirstOrDefault();
-
Application application = new Application();
-
return View(application);
-
}
-
-
[HttpPost]
-
[ValidateAntiForgeryToken]
-
public ActionResult Application([Bind(Include = "IdApplication,Name,Surname,Adress,Email,Telephone")] Application application)
-
{
-
Course course = new course();
-
application.IdCourse = course.IdCourse;
-
int applicationId = application.IdApplication;
-
-
if (ModelState.IsValid)
-
{
-
db.Applications.Add(Application);
-
db.SaveChanges();
-
return RedirectToAction("Index");
-
}
-
-
return View(application);
-
}
-
My view -
@model Aplikacija.Models.Applicaton
-
-
@{
-
<strong>@ViewBag.Course</strong>
-
}
-
-
-
@using (Html.BeginForm())
-
{
-
@Html.AntiForgeryToken()
-
-
<div class="form-horizontal"> <hr />
-
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
-
@Html.HiddenFor(model => model.IdApplication)
-
-
<div class="form-group">
-
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
-
</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>
-
}
-
-
<div>
-
@Html.ActionLink("Back to List", "Index")
-
</div>
-
-
@section Scripts {
-
@Scripts.Render("~/bundles/jqueryval")
-
}
2 2337 zmbd 5,501
Recognized Expert Moderator Expert
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...
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. - [HttpPost]
-
[ValidateAntiForgeryToken]
-
public ActionResult Application(Application application)
-
{
-
application.Course = new Course();
-
if (ModelState.IsValid)
-
{
-
db.Applications.Add(application);
-
db.SaveChanges();
-
return RedirectToAction("Index");
-
}
-
-
ViewBag.Course = new SelectList(db.Courses, "IdCourse", "NameCourse", application.IdCourse);
-
return View(application);
-
}
Thank you for your reply.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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) "_
& "SELECT '" & handid & "' As handid, '" _
& ArticleDate & "' As ArticleDate, '" _
& sport & " As sport, " _
& articleheader & "' As articleheader, '"...
|
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
'FK_tblOfficePageContent_tblPageContent'. The conflict occurred in database
'CPNCMS', table 'tblPageContent', column 'pageID'. The statement has been
terminated.
I cant seem to see...
|
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 :
INSERT INTO temp_tab VALUES (1,2,3)
|
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 "dbo.ProjectQuestion", column 'ProjectQuestionId'.
The statement has been terminated.
public void ProjectResponseandRespondentToDB(DataRow...
|
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 noticed that th eaccess plan for the 2 situations is quite
different. For the case where the trigger is in place, the optimizer
applies 2 extra residual...
| |
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 conflicted with the FOREIGN KEY constraint
"FK_ATM_Accounts". The conflict occurred in database "G:\DOCUMENTS AND...
|
by: haiminnu |
last post by:
I have created Two tables
1]EmployeeTable
--------------------------
EmpID
EmpName
AccessLevelID
2]AccessLevelTable
-----------------------------
|
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 am getting Error No. -2147217873 at Line No. 0 (The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblDetail_tblCompany". The...
|
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 is the project.ID and foreign key is in proposal.ID
and thses tow column proposal.ID and project.id does not allow null.
the problem is there is...
|
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 been terminated.
Hi, all. This is my maiden post.
Some background:
I am doing a C# project using Entity Framework 4.0 in Visual Studio 2010 and SQL...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |