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.
null.ex.ToString()instead ofex.Messageto get the full stack trace (or logging it somewhere if you don't want that detail visible to the end user).