Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have list of empid stored in var variable c#. i want to get the values from that variable. how i can do this.
eg:

C#
var empid=viewstate["empid"];

now the empid contains three empid like emp1,emp2,emp3 how can i retrieve each values that present in this var variable.
can anybody explain with example

Thanks
parithi
Posted
Updated 3-Jul-12 2:09am
v2

See if this helps,

XML
List<double> d = new List<double>();
 d.Add(1);
 d.Add(2);
 d.Add(3);
 d.Add(4);

var v = d;

List<double> newVal = (List<double>)v;
 
Share this answer
 
v2
like this:

C#
var empid = viewstate["empid"];

foreach(int employeeID in (List<int>)empid)
{
    Console.WriteLine(employeeID);
}
 
Share this answer
 
Comments
Mohamed Mitwalli 3-Jul-12 8:23am    
5+
Hi ,
Check this
First you have to Cast your ViewState
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        List<string> st = new List<string>();
        st.Add("me");
        st.Add("mm");
        st.Add("ee");
        st.Add("rr");
        ViewState.Add("st", st);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (ViewState["st"] != null)
    {
        List<string> stView = (List<string>)ViewState["st"];
        foreach (string item in stView)
        {
            TextBox1.Text += item;
        }
    }

}

Best Regards
M.Mitwalli
 
Share this answer
 
Hi,

from your code it looks like you are storing string value in viewState. if it is so then below solution may help you,

C#
string strEmployee = ViewState["empIds"]?? "";   //ViewState contain "emp001,emp002,emp003";
string[] empIds = strEmployee.Split(',');

foreach (var item in empIds)
{
    // iterate for each item and process further.
}


hope this may help you,

thanks
-Amit.
 
Share this answer
 

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