Whats the best way to use DataContext for reading and updating objects in ASP.Net
I was wondering what is the best way to use the DataContext in a ASP.Net application.
I want to do a very simple thing. I get a object from the database and view it to the user. The user can do some changes and save them back to the database. But the save needs a postback and so I don't have the DataContext I used for reading the data.
So I see two possibilities:
1.) Read and write with different Instances of the DataContext
Read the object from the DataContext and store it to a session variable. Allow the user to manipulate the data in the session. At the Save-Method create the DataContext again and use it to write the data. But here I have the problem, that the DataContext does not recognize this as an Update, it tries to make an Insert. I have this code:
{ dataContextProducts.GetTable< dataContextProducts.SubmitChanges(); }DataContext dataContextProducts =newDataContext("Data Source=NOTEBOOK_VISTA;Initial Catalog=ValuePlanner_2008;Integrated Security=True");
So how will I bring the DataContext to write thi2s as an Update? I thing reading the data again from the database, change them and write them back is not a good solution.
2.) Keep the DataContext in a session variable
The second way I was thinking about is to put the whole DataContext to a session variable and to reuse it for the save.
What do you think? What's the best way to do this? Are there any other possibilities?

