Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

In my web application, I want to give the user the ability to drill down the information. At first the user sees a car make and model, by clicking on the car make, the user can then see more information about the car such as year, color, etc. Then by clicking on the car year, the user can see more information like milage, e-test, and etc.

My question is how can I pull all the information about the car in advance and save it in a local object so that by clicking on the links (drilling down) I don't have to submit a new query to the database to pull the relevant information. I just want to be able to query the local object instead of the database.

Any help would be appreciated!

Thanks,

Jerry
Posted
Updated 23-Aug-10 7:39am
v2

Using SessionState is the solution.

Let us assume that you have a Car object which some basic properties, along with a CarDetail object property, something like as follows:

C#
public class Car
{
  public string Make {get;set;}
  public string Model {get;set;}
  //Other basic properties
  public CarDetail {get;set;}
}


The CarDetail class may have some detail properties as follows:

C#
public class Car
{
  public int Year {get;set;}
  public Coler coler {get;set;}
  //Other detail properties...
}


And, the CarDetail may have another objects as it's property.

Let's call this an object graph, where the Car object contains other object (CarDetail) which may contain other object(s) as property.

So, what you have to do is, when your page is loaded first, you load all basic and detailed information for the requested Car from the database and populate the data in the Car object graph. Then, you store the Car object in the Session. For later page request, you serve the Car object data from the Session.

Here is a sample method that does this:

protected Car GetCar(int carId)
{
    Car car = null;
    if(Session["Car"] == null)
    {
        car = GetCar(carId); //Populate the Car object graph from database
        Session["Car"] =  car; //Store in the session
    }
    else
    {
        car = Session["Car"] as Car; //Get the Car object from Session
    }
    return car;
}


Hope, this helps
 
Share this answer
 
Comments
#realJSOP 23-Aug-10 12:11pm    
I don't think he's working n a web site, so session state is not appropriate.
Al-Farooque Shubho 23-Aug-10 12:17pm    
"In my web application I want to give the user the ability to drill down the information.." that's what he wrote. So, he is definitely working on a web application/site and hence the Session state is most appropriate to use here.
jerrymei 23-Aug-10 13:44pm    
Thanks Farooque for the reply. I think using this method is good only if you are dealing with one object at a time. If you your are working with a collection of objects then this might cause a little bit of problem.
Al-Farooque Shubho 23-Aug-10 13:50pm    
Well, not true. If you are using a collection of objects, then, this approach is more appropriate. Because, a collection of objects means more data and the more you can cache it in session the better you gain performance :)

Even if you use DataTable, as HTTP is stateless, the DataTable will be lost when page execution is complete. So, you need to put the DataTable in the session, if you do not want to further hit the database on subsequent requests (As you mentioned in your original question).
Well, once you retrieve the data, it resides in a DataTable, correct? So just use LINQ to query the DataTable object.
 
Share this answer
 
Comments
Al-Farooque Shubho 23-Aug-10 12:19pm    
As he is using a Web application, the DataTable will be simply lost when the page execution is complete, and at each request the DataTable has to be re-populated from the database. So, he needs to store the DataTable in some state, say, SessionState.
jerrymei 23-Aug-10 13:46pm    
Hello John,

I like this method better. In fact this is exactly what I was looking for. In this case the DataTable would be the local object I was asking about and now I will look into LINQ capabilities to see if I can send parametrized queries to the DataTable using LINQ or not.

Thanks a lot!

Jerry

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