Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件上传,但数据不存在';不要进入数据库_C#_Asp.net_Database - Fatal编程技术网

C# 文件上传,但数据不存在';不要进入数据库

C# 文件上传,但数据不存在';不要进入数据库,c#,asp.net,database,C#,Asp.net,Database,所以,我有一个我正在处理的页面,我想上传一个文件,然后保存文件名,还有一些东西要保存到数据库中。文件最终保存到服务器,但文件名、“标签”和“节”不会插入到数据库中。下面是隐藏的代码: protected void UploadFile(object sender, EventArgs e) { TextBox txtDocLabelText = (TextBox)Formview1.FindControl("DocLabelText"); DropDownList ddlSect

所以,我有一个我正在处理的页面,我想上传一个文件,然后保存文件名,还有一些东西要保存到数据库中。文件最终保存到服务器,但文件名、“标签”和“节”不会插入到数据库中。下面是隐藏的代码:

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);
}
受保护的无效上载文件(对象发送方,事件参数e)
{
TextBox txtDocLabelText=(TextBox)Formview1.FindControl(“DocLabelText”);
DropDownList ddlSection=(DropDownList)Formview1.FindControl(“DropDownList1”);
FileUpload FileUpload1=(FileUpload)Formview1.FindControl(“FileUpload1”);
标签UploadStatusLabel=(标签)Formview1.FindControl(“UploadStatusLabel”);
if(FileUpload1.HasFile)
{
尝试
{
if(FileUpload1.PostedFile.ContentType==“应用程序/文档”||
FileUpload1.PostedFile.ContentType==“appl/text”||
FileUpload1.PostedFile.ContentType==“应用程序/vnd.msword”||
FileUpload1.PostedFile.ContentType==“应用程序/vnd.ms word”||
FileUpload1.PostedFile.ContentType==“应用程序/winword”||
FileUpload1.PostedFile.ContentType==“应用程序/word”||
FileUpload1.PostedFile.ContentType==“应用程序/msword”||
FileUpload1.PostedFile.ContentType==“应用程序/x-msw6”||
FileUpload1.PostedFile.ContentType==“应用程序/x-msword”||
FileUpload1.PostedFile.ContentType==“应用程序/pdf”||
FileUpload1.PostedFile.ContentType==“应用程序/x-pdf”||
FileUpload1.PostedFile.ContentType==“application/vnd.openxmlformats of iceDocument.wordprocessingml.document”||
FileUpload1.PostedFile.ContentType==“application/vnd.openxmlformats of iceDocument.wordprocessingml.template”
)
{
if(FileUpload1.PostedFile.ContentLength<10240000)
{
字符串filename=Path.GetFileName(FileUpload1.filename);
FileUpload1.SaveAs(Server.MapPath(“~/docs/HRDocs/”)+文件名);
UploadStatusLabel.Text=“上传状态:完成!”;
string constr=@“Provider=Microsoft.Jet.OLEDB.4.0;数据源=|DataDirectory\[database name];”;
string cmdstr=“插入文档(文件名、标签、节)值(?,?)”;
OLEDB连接con=新OLEDB连接(cont);
OleDbCommand com=新的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();
}
其他的
UploadStatusLabel.Text=“上传状态:文件必须小于100 MB!”;
}
其他的
UploadStatusLabel.Text=“上传状态:仅接受JPEG文件!”;
}
捕获(例外情况除外)
{
UploadStatusLabel.Text=“上载状态:无法上载文件。出现以下错误:“+ex.Message;
}
}
重定向(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显示它。

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

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

如果在使用.ToString()时仍然没有捕获下拉列表值,则可能存在回发问题,可以使用PreRender捕获该值。有关如何使用PreRender的信息,请参阅此链接:

您使用的是access数据库吗?我使用。这有关系吗?你有没有设定一个断点并逐步完成代码?隔离失败的行可以帮助您找到解决方案。是的,当我设置断点时,我的变量“filename”中确实存储了文件名,但当我转到“com.Parameters.AddWithValue(“@Section”,ddlSection.SelectedValue)”时,它的值只是“{System.Web.UI.WebControls.TextBox}”。