Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to update my a record using Entity framework 6 with asp.net webform.
I create repository for the Language class in entity framework so I can call them from outside .
this is my code:



what can I do, and what is the problem with my code?

What I have tried:

public static bool Update(Language lang)
{
try
{
using(QuranDBEntities context = new QuranDBEntities())
{
Language _lang = context.Languages.Find(lang.LanguageID);
_lang = lang;
context.Languages.Attach(_lang);
context.SaveChanges();
}
return true;
}
catch(Exception ex)
{
Auth.Document_Exception(ex);
return false;
}
}


but the code through an exception :
C#
System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.




I call the function using this code:
Language x;
                    x = RepLanguage.Get(_langID);
                    x.Name = txt_Name.Text;
                    x.Lang = txt_Lang.Text;
                    x.IsActive = bool.Parse(ddl_IsActive.SelectedValue);
                    x.CreationDate = lbl_CreationDate.Text;
                    x.LastModifiedDate = DateTime.Now.ToShortDateString() + "";
                    RepLanguage.Update(x);
                }
Posted
Updated 8-Mar-16 3:45am
v2
Comments
Sascha Lefèvre 8-Mar-16 9:26am    
Did you observe at which line the exception occured?
ENG.Samy Sammour 8-Mar-16 9:31am    
it is in the line where I copy the new object to the object from the database
ENG.Samy Sammour 8-Mar-16 9:35am    
please check the edit in the post

1 solution

Finally, I found the Solution.

it was in the way I call the function:

C#
Language x;
                    // ============== x = RepLanguage.Get(_langID);
    x = new Language();
    x.LanguageID = int.Parse(txt_LanguageID.Text);
    x.Name = txt_Name.Text;
    x.Lang = txt_Lang.Text;
    x.IsActive = bool.Parse(ddl_IsActive.SelectedValue);
    x.CreationDate = lbl_CreationDate.Text;
    x.LastModifiedDate = DateTime.Now.ToShortDateString() + "";
    RepLanguage.Update(x);
}</pre>
 
Share this answer
 
v2

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