Click here to Skip to main content
15,791,539 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
C#
[HttpPost]
public ActionResult Solution(List<Questionsmodel> model)
{
    if (ModelState.IsValid)
    {                
        ALBulkTestResult obj = new ALBulkTestResult();                
        var now = DateTime.Now;
        var date = new DateTime(now.Year, now.Month, now.Day,
                                now.Hour, now.Minute, now.Second);
        foreach (QuestionsModel ans in model)
        {                    
           
            obj.TestID = ans.TestId;
            obj.SkillID = ans.SkillId;
            obj.QuestionID = ans.ID;
            obj.CandidateAns = ans.SelectedAnswer;
            obj.TestDate = date;
        }
        //db.Entry(obj).State = EntityState.Modified;
        db.ALBulkTestResults.Add(obj);
        db.SaveChanges();
    }
    return RedirectToAction("Successfull");
}

this is the right wasy to the save multiple records. Please correct me..
Here i'm getting error
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code Additional information: An error occurred while updating the entries. See the inner exception for details.

C#
the insert statement conflicted with the foreign key constraint entity framework
Posted
Updated 29-Nov-15 9:42am
v3
Comments
xszaboj 29-Nov-15 9:37am    
and what is inner exception?
[no name] 29-Nov-15 11:25am    
Where is the context for db? and where is your query?
[no name] 29-Nov-15 11:26am    
What you have there is 80% done (looking good so far)
jagan12013 29-Nov-15 14:07pm    
In the list returning 20 questions and each question having 4 options.
while submitting data i need to save each question answer using the entity framword..
db is a datacontext object.

public ActionResult Solution()
{
List<questionsmodel> items;

decimal testid = 1;
items = (from p in db.ALBulkTestDetails
where p.TestID == testid
select new QuestionsModel
{
ID = p.QuestionID,
SkillId = p.SkillID,
QuestionText = p.Question,
Options = (from a in db.ALBulkTestDetails
join b in db.ALBulkTestDetails
on new { aa = a.QuestionID, a.SkillID } equals new { aa = b.QuestionID, b.SkillID }
where b.QuestionID == p.QuestionID && b.SkillID == p.SkillID
select new Answer
{

AnswerId = b.AnswerId,
Option1 = b.Option1,
Option2 = b.Option2,
Option3 = b.Option3,
Option4 = b.Option4
}).ToList()
}).Take(3).ToList();
return View(items);
}

this is my query returning to the view


while submitting post back list question model, i'm getting error message like
"the insert statement conflicted with the foreign key constraint entity framework"
Krunal Rohit 29-Nov-15 23:55pm    
You sure you're getting values in all the foreign key for your table ALBulkTestResult ?
Debug this code & check what values are coming in List<questionsmodel> model. Because it's hard to tell without knowing your table structure.


-KR

1 solution

First of all, the code below seems a bit redundant to me
C#
var now = DateTime.Now;
var date = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);

wouldn't it be better to just have this line?
C#
DateTime date = DateTime.Now;


Maybe I am missing something, but I fail to see how you will save more than one result using this code
C#
foreach (QuestionsModel ans in model)
{
    obj.TestID = ans.TestId;
    obj.SkillID = ans.SkillId;
    obj.QuestionID = ans.ID;
    obj.CandidateAns = ans.SelectedAnswer;
    obj.TestDate = date;
}

For each iteration of the loop, you change the properties of the single instance obj
Shouldn't this be an array?

As for the error you get.
Have you tried to skip the loop and just save one row?
As you don't show your table structure, I can only guess that ans.ID is a foreign key and this ID doesn't exist.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900