发生以下错误:对象引用未设置为对象的实例

本文关键字:设置 对象 实例 对象引用 错误 | 更新日期: 2023-09-27 17:53:29

我正在尝试上传文件,但无法获得它。下面的错误产生在我的"UploadStatusLabel"在我的实际页面:

"上传状态:文件无法上传。发生以下错误:对象引用未设置为对象的实例。"

下面是后面的代码:
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;
        }
    }

这里是标记:

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

页面本身加载良好,它只是当我试图上传文档,无论是pdf或docx。顺便说一下,如果我尝试上传任何类型的文件,而不是上面列出的文件,那么upload Status标签就会正确更新,因此它确实看起来正在通过验证。

发生以下错误:对象引用未设置为对象的实例

当我在代码中声明下拉列表时,我只是把错误的控件ID放在了我发布的上面。我想这就是我没有发布我的全部代码的后果。下面是工作代码:

后台代码:

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");

标记:

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

可能PostedFile属性为空。自从我使用fileupload控件以来已经有一段时间了,但是如果我没弄错的话,当你提交页面时,发布的文件可能会丢失。

检查这里:文件上传不工作