Solved

Store the Data Table in Viewstate

Posted on 2009-05-05
2
610 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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