0

I saved fileupload control in session. But when I am retrieving in another page(ie to know whether it has file or not),it is showing error as "object instances not set to an object". Where can be the fault? The code I used to get that fileupload control is

{
   Fileupload myupload=(Fileupload)Session["Fileupload1"];
   if(myupload.HasFile)
    {
          //some code
    }
}
3
  • Don't ever put controls to the Session. Why do you need to 'save' the control? Isn't it on your web form? Commented May 24, 2011 at 4:52
  • Yep, put the file content somewhere safe if you need to access it from multiple places. Commented May 24, 2011 at 4:53
  • you gotta store the file somewhere temporarily ! Commented May 24, 2011 at 4:53

3 Answers 3

3

Well that's not going to work. You can't put controls in session state. You'll need to process the file upload on the page that received the post. Then you'll need to save off the file to a temporary directory or something on the server.

I'd go back to the drawing board and try another approach.

Sign up to request clarification or add additional context in comments.

Comments

0

Without seeing the actual stack trace, it looks like Session["Fileupload1"] is null.

Comments

0

I would say that if you are saving the fileUpload to session, it's maybe not the best way to tackle your given problem, of which we know little about.

That said, you should check to see if an object is in session before casting it as something in case it has been lost.

if (Session["MySessionVar"] != null)
{
      <type> myVar = (<type>)Session["MySessionVar"]; 
}
else
{
      // set default/write warning to log/warn user
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.