?
Solved

Store the Data Table in Viewstate

Posted on 2009-05-05
2
Medium Priority
?
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
Krummelz earned 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 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