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:

Code Snippet

publicvoid SaveProductVersion(cProductVersion _ProductVersion)

{

DataContext dataContextProducts =newDataContext("Data Source=NOTEBOOK_VISTA;Initial Catalog=ValuePlanner_2008;Integrated Security=True");

dataContextProducts.GetTable<cProductVersion>().Add(_ProductVersion);

dataContextProducts.SubmitChanges();

}

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?

[2458 byte] By [tschissler] at [2008-1-8]
# 1

It is pretty clear in the examples you will see online as well as the recommendations posted here that you should not put the context in a session variable, but to put your instance object in the session. Since the context not only stores your value, but the original and current value of all objects fetched through that context instance, the datacontext is not scalable. The general concensus is to keep the context short lived.

I cover updating objects in ASP in part 2 of my ThinqLinq webcast series. See http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2007/07/09/asp-net-podcast-show-96-jim-wooley-on-link-part-ii-video-and-audio.aspx.

Jim Wooley

http://devauthority.com/blogs/jwooley

http://www.LinqInAction.net

jwooley at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 2

Hi Jim,

thanks, that was exactly what I was looking for!

tschissler at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 3
Hi Jim;

Question about your book. Amazon says your book will be available in Jan 2008. Is this correct info?
Secondly, is your eBook complete on the Linq to SQL and the subjuct of using Data Contex in ASP.Net apps?

I watched your video and got the idea, but a I need a slower pace info than a quick demo.

Thanks!

BenHayat at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 4

I don't know if the firm publication date has been set yet. I'm putting the finishing touches on the LINQ to SQL portion this weekend following the Beta 2 release. It's not part of the eBook yet, but should be coming soon.

I realize the pace is a bit quick on the webcast. Feel free to download the sample project at http://devauthority.com/files/13/jwooley/entry38500.aspx and play with it yourself. The sample online was done with the March CTP. I need to update it soon to beta 2. I'll be presenting it next weekend at the Jacksonville FL Code Camp, so I can probably update it for that.

Jim

http://LinqInAction.net

jwooley at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 5
jwooley wrote:

I don't know if the firm publication date has been set yet. I'm putting the finishing touches on the LINQ to SQL portion this weekend following the Beta 2 release. It's not part of the eBook yet, but should be coming soon.

I realize the pace is a bit quick on the webcast. Feel free to download the sample project at http://devauthority.com/files/13/jwooley/entry38500.aspx and play with it yourself. The sample online was done with the March CTP. I need to update it soon to beta 2. I'll be presenting it next weekend at the Jacksonville FL Code Camp, so I can probably update it for that.

Jim

http://LinqInAction.net

Hi Jim;

Could you please post a message on your blog when your eBook has been updated and I can then order the eBook.

Will you be giving the same speech at Jacksonville as you did in Atlanta?
Looking forward to getting the samples update and hopefully your new webcast.
I'll be monitoring your blog!

Thanks for the reply Jim!

BenHayat at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 6

I'm giving the ThinqLinq talk that you see in the webcast series. I'm also putting together a new VB9 language features talk which will be somewhat divorced from LINQ.

I'll be sure to announce when my chapters are available online. Make sure to sign up with the following link so I can get my commission cut ;-) http://www.manning.com/affiliate/idevaffiliate.php?id=263_74

Jim Wooley

http://www.LinqInAction.net

jwooley at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 7
>>Make sure to sign up with the following link so I can get my commission cut ;-)

Done! ;-)

BenHayat at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...

Visual Studio Orcas

Site Classified