Search code examples
c#asp.netviewstateasyncfileupload

Not getting value in Viewstate in asp.net using C#?


I am using a asyncfileupload control to upload a file there i am taking the path in a view state like this:

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string name = System.IO.Path.GetFileName(e.FileName); 
    string dir = Server.MapPath("upload_eng/");
    string path = Path.Combine(dir, name);
    ViewState["path"] = path;
    engcertfupld.SaveAs(path);
}

Now when i am trying to save that path in a buttonclick event i am not getting the value of viewstate:

protected void btnUpdate_Click(object sender, EventArgs e)
{
   string filepath = ViewState["path"].ToString(); // GETTING NULL in filepath
}

In this filepath i am getting null actually i am getting error NULL REFERENCE EXCEPTION

What can I do now?


Solution

  • Put the Path value in the Session object instead of the ViewState, like this:

    protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
    
        ....
        string path = Path.Combine(dir, name);
        Session["path"] = path;
    }
    

    Then in the Button Click:

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
      if (Session["path"] != null)
      {
         string filepath = (string) Session["path"];
      }
    }