0

Visual Studio 2013, ASP.NEt MVC 5 Identity Please someone help me in how to save the information in the database through controller. Let me explain...I want the following to happen: The user when logged is able update his Education information. After making changes in the fields, he will press the save button and the information is saved in the database. This works fine when i am doing so with the default aspnet users class, i take the usermanager. update method and the info is saved, but i am unable to do so with for any other table. I would be really thankful if someone helps me. Here's the controller class edit method

   public async Task<ActionResult> Edit(string id, Education education)
    {

        if (!User.Identity.IsAuthenticated)
        {
            Response.Redirect("~/Account/Login");
        }

        var db = new ApplicationDbContext();
        var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);

        educationdb.Qualification = education.Qualification;
        educationdb.School = education.School;
        educationdb.SchFrom = education.SchFrom;
        educationdb.SchTo = education.SchTo;
        educationdb.College = education.College;
        educationdb.ClgFrom = education.ClgFrom;
        educationdb.ClgTo = education.ClgTo;
        educationdb.University = education.University;
        educationdb.UniFrom = education.UniTo;
        educationdb.Description = education.Description;


        db.Entry(educationdb).State = System.Data.Entity.EntityState.Modified;
        await db.SaveChangesAsync();
        //return RedirectToAction("Index");
        return View();
    }

this is the model class:

namespace theme1.Models
{
public class Education
{
    public string EducationID { get; set; }

    public string UserID { get; set; }
    public ApplicationUser User { get; set; }

    public string Qualification { get; set; }

    public string School { get; set; }
    [DataType(DataType.Date)]
    public DateTime SchFrom { get; set; }
    [DataType(DataType.Date)]
    public DateTime SchTo { get; set; }

    public string College { get; set; }
    [DataType(DataType.Date)]
    public DateTime ClgFrom { get; set; }
    [DataType(DataType.Date)]
    public DateTime ClgTo { get; set; }

    public string University { get; set; }
    [DataType(DataType.Date)]
    public DateTime UniFrom { get; set; }
    [DataType(DataType.Date)]
    public DateTime UniTo { get; set; }

    public string Description { get; set; }
 }
}

var educationdb = db.Edu.First(u => u.EducationID == education.EducationID); this line gives me an error Exception Details: System.InvalidOperationException: Sequence contains no elements

Source Error: Line 109: var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);

ajkiah
  • 9
  • 4

3 Answers3

0

Instead of :

var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);

Try this :

var educationdb = db.Edu.Where(u => u.EducationID == education.EducationID).FirstOrDefault();

OR

Instead of :

var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);

Try this :

var educationdb = db.Edu.FirstOrDefault(u => u.EducationID == education.EducationID);
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
  • thanks for the instant help, now i have got another error System.NullReferenceException: Object reference not set to an instance of an object. on this line educationdb.Qualification = education.Qualification; – ajkiah Aug 19 '14 at 11:56
  • yes the previous one is gone, but my goal hasn't been achieved as yet...the changes must be saved in the database – ajkiah Aug 19 '14 at 12:00
  • @ajkiah..check ..education.Qualification its value is null that is why error is coming.. – Kartikeya Khosla Aug 19 '14 at 12:02
  • yes, i know...nothing is stored in this table, i tried doing u.UserID==education.UserID, this should take the userid of the logged in user and the table should store values when the user is making changes, but the same nullref exception. kindly let me know what should i do to get them saved...thanks – ajkiah Aug 19 '14 at 12:06
  • @ajkiah...sorry but i m not getting you .. i just corrected your problem as given in your question..now from where u.UserID come from ..???? – Kartikeya Khosla Aug 19 '14 at 12:09
  • the thing is that i just want the information to be stored in the database. – ajkiah Aug 19 '14 at 12:11
  • you can try educationdb.UserId=education.UserID – Kartikeya Khosla Aug 19 '14 at 12:11
  • "cannot use a local variable educationdb before it is declared" :( – ajkiah Aug 19 '14 at 12:16
  • write it after..educationdb.Description = educationdb.Description; – Kartikeya Khosla Aug 19 '14 at 12:17
0

I suspect that your missing the DBSet for Education model in ApplicationContext.

If this doesn't fix it, could you please provide the code for your ApplicationContext class?

TheMagnificent11
  • 1,424
  • 4
  • 19
  • 40
0

I cant comment on your discussion with Kartikeya since I am new to stackoverflow and lack reputation.

But after you changed your lambda:

var educationdb = db.Edu.Where(u => u.EducationID == 
education.EducationID).FirstOrDefault();

From your discussion with Kartikeya It sounds like you are thinking of Creating rather than editing. You cant edit something that doesn´t exist.

    // GET: Model
    public ActionResult Create()
    {
         return View();
    }

in MVC 5 it is very easy to scaffold the view by right clicking inside that Action and chosing "add view" and insert the information you need, and when you have created the view, create your post method for Create:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include ="EducationID, UserId....")] Education education)
    {
        if (ModelState.IsValid)
        {
            //any extra logic you might want

            db.Edu.Add(education);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
    return View(education);
    }
Mikael Puusaari
  • 854
  • 1
  • 10
  • 14