views:

62

answers:

3

I am a VB.net winforms programmer attempting to build an ASP.Net app. I use data classes(objects) through reflection in most of my vb projects and was trying to adapt it to ASP.net using the VB code behind. I have a webpage that serves as an add/edit page for contact info. I instatiate my class which grabs the contact data from the data base then I have a process that loops through the controls on the form and matches up with a property in the data class. I can display data no problem. When I edit data and click the submit button my code calls a then loops through the controls on the form again and matches the control to the property of the data class to update the property of the class. However, my data class is no longer valid. I know web programming is different then winforms but I can't seem to get over the hump on this one. Is this the wrong way to go about this? Is my data class only available on the server side? Do I just reinstantiate the initial class and then loop through the propeties and change what the user changed and then call the update method (see redundant)? How can I get data class into a session object (I made an attempt in the past but was under tight deadlines and had to abandon it, maybe I need to revisit it?)?

Thanks

A: 

You need to save your data class somewhere, usually in a session variable, otherwise it goes away as soon as the page gets sent back the user. Or else you need to recreate the data class again upon posting.

Otávio Décio
+1  A: 

Hey,

Yes, you need to reload the data class from the database as one option, or use an alternative approach. The reason is web is stateless, so all local variables are destroyed then the server side page unload process occurs. This means that in between requests, you need something to store your data.

You can read/write an object via the Session colleciton, as so:

Session["A"] = myobj; myobj = (ObjType)Session["A"];

And so session stores an object for a specific user. Alternatively, cache stores application level data, so one instance of an object is available to all users (where session is unique to each user). You can make cache unique to a user by appending a user ID to the cache string.

var o = Cache.Get("A"); if (o != null) { .. }

Cache.Add("A", o, ...);

And so these mechanisms help you temporarily retain data.

Brian
Thanks for pointing me in the right direction.
VBCSharp
+1  A: 

If you decide to keep some of your data in Session, you owe it to yourself to look at this post. Your code will be much cleaner and easier to maintain.

TheObjectGuy
Thanks. I took a look at your post and it makes a lot of sense to me. I did run into the serializable issue you point out at the end but was attempting something different at the time. I will check it out.
VBCSharp
@VBCSharp Let me know how it works out for you. Honestly, I don't think there is a better or cleaner way to use Session than what I describe in my post.
TheObjectGuy
Thanks. It works like a charm. I didn't implement the way you do it yet but tested the Session object and it worked great.
VBCSharp