Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: list viewstate
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 = "Mary@somemail.com"});
        empList.Add(new Employee() { ID = 2, Email = "John@somemail.com"});
        empList.Add(new Employee() { ID = 3, Email = "Amber@somemail.com"});
            empList.Add(new Employee() { ID = 8, Email = "Jonay@somemail.com"});
        empList.Add(new Employee() { ID = 9, Email = "Robert@somemail.com"});
        empList.Add(new Employee() { ID = 10, Email = "Krishna@somemail.com" });
        return empList;
    }
Posted 16-May-11 0:28am
Edited 16-May-11 2:40am
v2

1 solution

Rate this: bad
good
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.
thanks,
Hemant
  Permalink  
Comments
praveen_gpk at 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 at 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.
 
Thanks,
Hemant
praveen_gpk at 16-May-11 10:53am
   
ok i got the solution have to use page inherit web.ui.page

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

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 /\jmot 189
4 Praneet Nadkar 173
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2014
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