C sharp - file upload issue

Posted on 2008-11-13
Last Modified: 2013-12-17
I have a form and it is supposed to upload files to the server.
Attached is the code:

This is the error I get  "{"Object reference not set to an instance of an object."}"


    <form id="form1" runat="server">




                <td colspan="3" style="height: 21px">

                    Upload Files</td>



                <td style="width: 100px">

                    Choose a file to upload</td>

                <td style="width: 100px">

                    <input id="File1" type="file" /></td>

                <td style="width: 100px">

                    <input id="Button1" type="button" value="Upload" runat="server" onserverclick="Button1_ServerClick" /></td>






C# Code Behind


using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 


    protected System.Web.UI.HtmlControls.HtmlInputFile File1;

    protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;

    protected void Page_Load(object sender, EventArgs e)



    protected void Button1_ServerClick(object sender, EventArgs e)


        if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))     <--- Error points here


            string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

            string SaveLocation = Server.MapPath("Upload") + "\\" + fn;




                Response.Write("The file has been uploaded.");


            catch (Exception ex)


                Response.Write("Error: " + ex.Message);

                //Note: Exception.Message returns a detailed message that describes the current exception. 

                //For security reasons, we do not recommend that you return Exception.Message to end users in 

                //production environments. It would be better to put a generic error message. 





            Response.Write("Please select a file to upload.");




Open in new window

Question by:Isaac
    LVL 3

    Accepted Solution


    <INPUT type=file id=File1 name=File1 runat="server" />
    LVL 5

    Author Comment

    Now I get this error:

    Compiler Error Message: CS0102: The type '_Default' already contains a definition for 'File1'
    Line 13:     protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    LVL 9

    Assisted Solution

    delete line 13
    Line 13:     protected System.Web.UI.HtmlControls.HtmlInputFile File1;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now