我已经编写了以下代码,当它只是将文件上传到文件夹时,一切都很好。我已将其更改为将文件名和文件路径插入数据库,但我收到错误消息:
名称为' DataUpload'的字段或媒体资源未在所选数据源中找到
DataUpload是文件夹名称,之前工作正常。我可能遗漏了一些简单的东西,但我没有看到它。
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
try
{
FileUpload1.SaveAs(Server.MapPath("DataUpload\\" + FileUpload1.FileName));
Guid newGUID = Guid.NewGuid();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string InsertUser = "INSERT INTO UserUpload (ID, Comment, FilePath, FileName) VALUES (@ID, @Comment, @FilePath, @FileName)";
SqlCommand com = new SqlCommand(InsertUser, conn);
com.Parameters.AddWithValue("@ID", newGUID.ToString());
com.Parameters.AddWithValue("@Comment", TextBoxComment.Text);
com.Parameters.AddWithValue("@FilePath", "DataUpload/" + FileName);
com.Parameters.AddWithValue("@FileName", FileName);
com.ExecuteNonQuery();
LabelMessage.Text = ("Your Upload Is Complete");
conn.Close();
}
catch (Exception ex)
{
LabelMessage.Text = ("Error:" + ex.Message);
}
}
答案 0 :(得分:0)
为您为FilePath创建的字符串添加单引号,例如:string.Format("'DataUpload/{0}'", FileName);