dhanya
dhanya

Reputation: 67

Problem with retrieveing file upload control

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
    }
}

Upvotes: 0

Views: 271

Answers (3)

Fellmeister
Fellmeister

Reputation: 591

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
}

Upvotes: 0

Josh
Josh

Reputation: 69282

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.

Upvotes: 3

Chris Farmer
Chris Farmer

Reputation: 25426

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

Upvotes: 0

Related Questions