我在通过 C# 实现更新和 SQL Server 时遇到问题。
情况是用户可能想要更新系统中的产品,如果没有选择图像,则保存图像路径的字段全部为 NULL。
我检查了 fileupload 控件是否有要上传的文件,但如果控件没有文件,那么它应该运行更新查询来更新剩余的字段,而不是与 fileupload 控件绑定的字段。
下面是页面背后的代码。
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)FormView1.FindControl("FileUpload3");
FileUpload FileUpload2 = (FileUpload)FormView1.FindControl("FileUpload4");
if (FileUpload1.HasFile)
{
string virtualFolder1 = "~/ProductArt/Thumb/";
string physicalFolder1 = Server.MapPath(virtualFolder1);
string oldkey = e.Keys["ItemID"].ToString();
//string photopath1 = e.OldValues["ThumbPath"].ToString();
string ext1 = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder1, oldkey + ext1));
//e.Values["ThumbPath"] = photopath1 + ext1;
e.NewValues["ThumbPath"] = virtualFolder1 + oldkey + ext1;
}
else
{
//throw new Exception("Error, no thumb file selected");
SqlConnection connect = new SqlConnection("TTEConnectionString");
connect.Open();
SqlCommand cmdQuery = new SqlCommand(@"UPDATE Item SET ItemName='" + txtItemNameTextBox.Text + "', PublisherID='" + ddlPublisher.SelectedValue );
}
if (FileUpload2.HasFile)
{
string virtualFolder2 = "~/ProductArt/";
string physicalFolder2 = Server.MapPath(virtualFolder2);
string oldkey2 = e.Keys["ItemID"].ToString();
//string photopath1 = e.OldValues["ThumbPath"].ToString();
string ext2 = System.IO.Path.GetExtension(FileUpload2.FileName);
FileUpload2.SaveAs(System.IO.Path.Combine(physicalFolder2, oldkey2 + ext2));
//e.Values["ThumbPath"] = photopath1 + ext1;
e.NewValues["PhotoPath"] = virtualFolder2 + oldkey2 + ext2;
}
else
{
throw new Exception("Error, no full box file selected");
}
}
此外,每当我输入一个 ASP 文本框的 ID 时,它都会给出
当前上下文中不存在名称 xxxx
错误。