I have the following class, of which I want to store a list inside a ViewState.
[Serializable]
public class Project
{
public string projectID { get; set; }
public string workerID { get; set; }
public string nameProject{ get; set; }
public Project(string pid, string wid, string npro)
{
projectID = pid;
workerID = wid;
nameProject= npro;
}
}
Within my aspx
I have the following method that fills a list with the projects that I get from a DataTable
. I finally store the list in a ViewState
for later use.
private void fillList(DataTable dt)
{
List<Project> projects = new List<Project>();
for (int i = 0; i < dt.Rows.Count; i++)
{
string pid = dt.Rows[i][0].ToString();
string wid = dt.Rows[i][1].ToString();
string npro = dt.Rows[i][2].ToString();
projects.Add(new Project(pid, wid, npro));
}
ViewState["Projects"] = new List<Project>();
ViewState["Projects"] = projects;
}
Now I want to retrieve the list
stored in the ViewState
, to perform other actions. This is what I have tried.
protected void btnRetreive_Click(object sender, EventArgs e)
{
List<Project> data = (List<Project>)ViewState["Projects"];
Response.Write(data.Count);
}
But when I run it I get the following error
An exception of type 'System.NullReferenceException' occurred in App_Web_vwe5qgkl.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
What am I doing wrong?