文件上传,但数据无法进入数据库

时间:2014-03-18 19:59:21

标签: c# asp.net database

所以,我有一个页面,我正在处理我要上传文件的位置,然后保存文件名,以及更多的东西到数据库。该文件最终保存到服务器,但文件名“label”和“section”不会插入到数据库中。这是代码隐藏:

protected void UploadFile(object sender, EventArgs e)
{
    TextBox txtDocLabelText = (TextBox)Formview1.FindControl("DocLabelText");
    DropDownList ddlSection = (DropDownList)Formview1.FindControl("DropDownList1");
    FileUpload FileUpload1 = (FileUpload)Formview1.FindControl("FileUpload1");
    Label UploadStatusLabel = (Label)Formview1.FindControl("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);
                    FileUpload1.SaveAs(Server.MapPath("~/docs/HRDocs/") + filename);
                    UploadStatusLabel.Text = "Upload status: Complete!";
                    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\[database name];";
                    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();
                }
                else
                    UploadStatusLabel.Text = "Upload status: The file has to be less than 100 MB!";
            }
            else
                UploadStatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch (Exception ex)
        {
            UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
    Response.Redirect(Request.RawUrl);
}

这是标记:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/[database name]" 
    DeleteCommand="DELETE FROM [Docs] WHERE [ID] = ?" 
    InsertCommand="INSERT INTO [Docs] ([Filename], [Label], [Section]) VALUES (?, ?, ?)" 
    SelectCommand="SELECT * FROM [Docs]" 
    UpdateCommand="UPDATE [Docs] SET [Filename] = ?, [Label] = ?, [Section] = ? WHERE [ID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Filename" Type="String" />
        <asp:Parameter Name="Label" Type="String" />
        <asp:Parameter Name="Section" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Filename" Type="String" />
        <asp:Parameter Name="Label" Type="String" />
        <asp:Parameter Name="Section" Type="String" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
</asp:AccessDataSource>

<asp:FormView ID="Formview1" runat="server" DefaultMode="Insert" DataSourceID="AccessDataSource1" >
    <InsertItemTemplate>
        <br />
        <asp:Label ID="DocLabelLabel" runat="server" Text="What is the label? (can only be Word or PDF documents):" /><br />
        <asp:TextBox ID="DocLabelText" runat="server" Columns="100" /><br /><br />
        <asp:DropDownList ID="DropDownList1" runat="server" 
            DataSourceID="AccessDataSource1" DataTextField="Section" 
            DataValueField="Section">
        </asp:DropDownList>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Label ID="UploadStatusLabel" runat="server" Text="Status: " /><br />
        <asp:Button ID="UploadButton" runat="server" OnClick="UploadFile" Text="Insert Item" /><br />
    </InsertItemTemplate>
</asp:FormView><br />

同样,所有发生的事情都是文件被上传到服务器,但信息没有被放入数据库。我在页面下方有一个gridview控件,显示数据库中的信息,但它甚至没有进入数据库以供gridview显示。

1 个答案:

答案 0 :(得分:0)

尝试使用.ToString()来捕获下拉列表值。另外,在传递参数之前捕获值以使其更容易调试。

string section = ddlSection.SelectedValue.ToString();
com.Parameters.AddWithValue("@Section", section);

如果在使用.ToString()时仍未捕获下拉列表值,则可能会出现回发问题,并且可以使用PreRender捕获值。有关如何使用PreRender的信息,请参阅此链接:http://www.codeproject.com/Questions/191898/DropDownList-and-capturing-values-after-SelectedIn