Click here to Skip to main content
15,891,845 members
Please Sign up or sign in to vote.
4.00/5 (1 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?
C#
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
Updated 16-May-11 1:40am
v2

1 solution

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
 
Share this answer
 
Comments
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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900