Roel Gielis
Roel Gielis

Reputation: 29

Get values out of a session and save it to database ASP.NET

I'm creating some sort of survey in ASP.NET MVC and when a user fills a create form view all the values are stored in an object that will be saved in a session. At the end of the survey I want to save all the objects to the database in one action.

Email mails = new Email();
mails = Session["Emails"];

db.Emails.Add(mails);

It says:

cannot implicitly convert type Object to Namespace.models.emails.

The error occurs at this line :

mails = Session["Emails"];

Upvotes: 0

Views: 240

Answers (1)

Saket Kumar
Saket Kumar

Reputation: 4835

Seesions in ASP.Net, by default, returns the Object type. You have to typecast it while assigning.

Try this:

if (Session["Emails"] != null) 
{       
   Email mails = (Email)Session["Emails"];  //Assuming Session will contain one email
   db.Emails.Add(mails);
}

Upvotes: 2

Related Questions