1

I am trying to upload a file and can't get it to. The following error generates in my "UploadStatusLabel" on my actual page:

"Upload Status: The file could not be uploaded. The following error occured: Object reference not set to an instance of an object."

Here is the code behind:

if (FileUpload1.HasFile)
    {
        try
        {
            if (FileUpload1.PostedFile.ContentType == "application/doc" ||
                FileUpload1.PostedFile.ContentType == "appl/text" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.msword" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.ms-word" ||
                FileUpload1.PostedFile.ContentType == "application/winword" ||
                FileUpload1.PostedFile.ContentType == "application/word" ||
                FileUpload1.PostedFile.ContentType == "application/msword" ||
                FileUpload1.PostedFile.ContentType == "application/x-msw6" ||
                FileUpload1.PostedFile.ContentType == "application/x-msword" ||
                FileUpload1.PostedFile.ContentType == "application/pdf" ||
                FileUpload1.PostedFile.ContentType == "application/x-pdf" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
                )
            {
                if (FileUpload1.PostedFile.ContentLength < 102400000)
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    string section = ddlSection.SelectedValue
                    FileUpload1.SaveAs(Server.MapPath("~/docs/HRDocs") + filename);
                    UploadStatusLabel.Text = "Upload status: Complete!";
                    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
                    string cmdstr = "INSERT INTO Docs (Filename, Label, Section) VALUES (?,?,?)";

                    OleDbConnection con = new OleDbConnection(constr);
                    OleDbCommand com = new OleDbCommand(cmdstr, con);

                    con.Open();
                    com.Parameters.AddWithValue("@Filename", filename);
                    com.Parameters.AddWithValue("@Label", txtDocLabelText.Text);
                    com.Parameters.AddWithValue("@Section", ddlSection.SelectedValue);
                    com.ExecuteNonQuery();
                    con.Close();
                    Response.Redirect("ManageHRDocs.aspx");
                }
                else
                    UploadStatusLabel.Text = "Upload status: The file has to be less than 100 MB!";
            }
            else
                UploadStatusLabel.Text = "Upload status: Not an accepted file type";
        }
        catch (Exception ex)
        {
            UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }

And here is the markup:

<asp:FormView ID="Formview1" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource1" DefaultMode="Insert">

    <InsertItemTemplate>
        Label:
        <asp:TextBox ID="LabelTextBox" runat="server" />
        <br />
        Section:
        <asp:DropDownList ID="ddlSection" runat="server" 
        DataSourceID="AccessDatasource2" DataTextField="Sections" DataValueField="Sections" />
        <br /><br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="UploadButton" runat="server" Text="Upload document" OnClick="UploadFile" /><br />
        <asp:Label ID="UploadStatusLabel" runat="server" Text="Upload Status: " />
    </InsertItemTemplate>

</asp:FormView>

The page itself loads fine, it's just when I try to upload a document, whether it be pdf or docx. On a side note, if I try to upload any type of file other than what is listed up there, then the Upload Status label updates properly, so it does indeed seem to be going through the validation.

12
  • 4
    While line is the error on? Something on that line is null. Commented Mar 26, 2014 at 14:34
  • I'm checking now. Hold on a sec. Commented Mar 26, 2014 at 14:36
  • You might try using ex.ToString() instead of ex.Message to get the full stack trace (or logging it somewhere if you don't want that detail visible to the end user). Commented Mar 26, 2014 at 14:42
  • In the code-behind further up, it was saying that my label that I was inputting was null. And it was because it was looking for "DocLabelText" instead of "LabelTextBox" in my form. But now it is giving me the same error and I can't find what is null using the breakpoints... Commented Mar 26, 2014 at 14:44
  • Label has a value, filename has a value, the dropdown list has a value... Commented Mar 26, 2014 at 14:48

2 Answers 2

1

I had simply put the wrong control ID when I declared the drop down list in code above what I posted. I guess that's what I get for not posting my entire code... Here is the code that works.

Code-Behind:

string filename = Path.GetFileName(FileUpload1.FileName);
string section = ddlSection.SelectedValue;
string label = txtDocLabelText.Text;
FileUpload1.SaveAs(Server.MapPath("~/docs/HRDocs") + filename);
UploadStatusLabel.Text = "Upload status: Complete!";
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO Docs (Filename, Label, Section) VALUES (?,?,?)";

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);

con.Open();
com.Parameters.AddWithValue("@Filename", filename);
com.Parameters.AddWithValue("@Label", label);
com.Parameters.AddWithValue("@Section", section);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageHRDocs.aspx");

Markup:

<asp:FormView ID="Formview1" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource1" DefaultMode="Insert">

    <InsertItemTemplate>
        Label:
        <asp:TextBox ID="LabelTextBox" runat="server" />
        <br />
        Section:
        <asp:DropDownList ID="ddlSection" runat="server" 
        DataSourceID="AccessDatasource2" DataTextField="Sections" DataValueField="Sections" />
        <br /><br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="UploadButton" runat="server" Text="Upload document" OnClick="UploadFile" /><br />
        <asp:Label ID="UploadStatusLabel" runat="server" Text="Upload Status: " />
    </InsertItemTemplate>

</asp:FormView>
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe, the PostedFile property is null. It has been a while since i worked with the fileupload control, but if i'm not mistaken, the posted file can be lost when you submit the page.

Check here: File Upload is not working

3 Comments

When I run through it with breakpoints, it keeps telling me the value of my drop down list is null.
It specifically returns the error saying that line 47 (the line that says "string section = ddlSection.SelectedValue" has a null reference exception,
Hmmm..by any chance, are you binding the datasource of this dropdownlist after you submit the page? For example: you're calling some method to fill the dropdownlist everytime on the page_load method. If i'm not mistaken, you loose the selectedvalue value if you bind the datasource. Correct me if i'm wrong.

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.