C sharp - file upload issue

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."}"
ASP.NET Form
-----------------------------------------------------------------------------------------
    <form id="form1" runat="server">
    <div>
        &nbsp;<table>
            <tr>
                <td colspan="3" style="height: 21px">
                    Upload Files</td>
            </tr>
            <tr>
                <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>
            </tr>
        </table>
    
    </div>
    </form>
 
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;
            try
            {
                File1.PostedFile.SaveAs(SaveLocation);
                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. 
            }
        }
        else
        {
            Response.Write("Please select a file to upload.");
        }
    }
}

Open in new window

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
cluebeckConnect With a Mentor Commented:
http://support.microsoft.com/kb/323246

<INPUT type=file id=File1 name=File1 runat="server" />
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
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;
0
 
amxConnect With a Mentor Commented:
delete line 13
Line 13:     protected System.Web.UI.HtmlControls.HtmlInputFile File1;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.