Solved

Store the Data Table in Viewstate

Posted on 2009-05-05
2
609 Views
Last Modified: 2012-06-27
I ran accross this code in a Project :

                 private string vsCounselors = "dtCounselors";

                   public void ReLoad(bool refresh)
        {
            // Define our variables.
            DataTable _dt = new DataTable();

            try
            {
                // Get the datatable containing counselors information.
                if (refresh)
                {
                    _dt = CodeLayers.BL.AdminCounselorBL.GetCounselors();
                    ViewState[vsCounselors] = _dt;
                }
                else
                {
                    _dt = ViewState[vsCounselors] as DataTable;
                }

                if (_dt.Rows.Count > 0)
                {
                    dlCounselors.DataSource = _dt;
                    dlCounselors.DataBind();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
What is ViewState[vsCounselors] variable?

dtCounselors is not a control on the page.


thanks
0
Comment
Question by:Faye_DBA
2 Comments
 
LVL 5

Accepted Solution

by:
Krummelz earned 500 total points
ID: 24314064
Viewstate is a type of storage space (an array) in .NET that is used in the background by .NET, to store data for controls accross postbacks.

So for example, if you have a textbox control and a drop down list which is set to cause an auto postback; and you just typed something into the textbox, and then selected an item on the drop down list. The drop down list will cause a post back, and when it does that, you textbox will not lose the data that you typed into it - because its saved in Viewstate automatically.

BUT, you are also able to use Viewstate as you wish to keep data across a postback. It works in much the same way as Session, except that each page has its own Viewstate - so one page's viewstate wont be the same as the next page's viewstate. The name "vsCounselors" in braces after the word Viewstate, is the name it uses to store your data under. You also use that name to retrieve the data you stored there.

So in your code example, a Datatable is being stored in a Viewstate variable called "vsCounselors" if the refresh variable is true. If refresh is false, it gets the value inside the "vsCounselors" Viewstate variable and stores it in a Datatable called "_dt".

If the Datatable "_dt" has rows, then it gets used as the datasource for "dlCounselors".
0
 

Author Closing Comment

by:Faye_DBA
ID: 31578153
Excellent. I did my own research and came up with the same answer.

thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now