Click here to Skip to main content
13,043,092 members (62,367 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Suppose I have a data in a list like below and now I want to get data from viewstate which already contain table in that I want one column. How can I do that?
public List<Employee> GetEmployeeList()
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee() { ID = 1, Email = ""});
        empList.Add(new Employee() { ID = 2, Email = ""});
        empList.Add(new Employee() { ID = 3, Email = ""});
            empList.Add(new Employee() { ID = 8, Email = ""});
        empList.Add(new Employee() { ID = 9, Email = ""});
        empList.Add(new Employee() { ID = 10, Email = "" });
        return empList;
Posted 15-May-11 23:28pm
Updated 16-May-11 1:40am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If i'm getting your question right you have a datatable in the viewstate and you want to add all the values from rows of a particular column to the list you have shown above. if that is the case you can:

1- Get Datatable from viewstate. DataTable dataTable = ViewState["myDataTable"]
2- Get the list from GetEmployeeList(...). List<Employee> listEmployee = GetEmployeeList();

3- Iterate through rows of datatable and add values to your list variable.

for(rowIndex=0; rowIndex<datatable.rows.count;>{
string columnValue = dataTable.Rows[rowIndex]["email"];
Employee emp = new Employee(){ID=someID, Email=columnValue});

Hope it will help.
praveen_gpk 16-May-11 6:23am
may be you got my question but their is error that am not able to access viewstate over their Error,,check out the error The name 'ViewState' does not exist in the current context
Hemant-Sharma-81 16-May-11 6:37am
Where are you trying to access the ViewState object? you cannot access viewstate outside it's page's scope.
It seems you have a .aspx page and a helper class where you have written GetEmployeeList and you are trying to access ViewState object outside the page. if that is the case you can fetch the datatable in your page itself and then send to GetEmployeeList(...) function or some target function as input patameter.

praveen_gpk 16-May-11 10:53am
ok i got the solution have to use page inherit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100