我正在制作一个gridview crud,它保存:关系、文件名、文件路径(都是varchar)在数据库中,以及网页中文件夹中的图像。
我已经可以添加,选择和删除,但麻烦来时,我试图更新。当我尝试更新时,我成功地修改了数据库,因为它只是文本。但是,当上传新图像时,旧图像仍然保留,我想在更新时删除旧图像,因此只有新图像保留在文件/文件夹“images”中,我应该尝试什么?
(更新后图像仍在)
这是我删除旧图像的非工作代码:
后端(只是更新方法)
protected void gvImages_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
HttpPostedFile file = ((FileUpload)gvImages.Rows[e.RowIndex].FindControl("FileUpload2")).PostedFile;
string fileName = Path.GetFileName(file.FileName);
MySqlConnection con = new MySqlConnection(strConnString);
string strQuery = "update testingdb.country set Relation=@Relation, FileName=@FileName, FilePath=@FilePath where idcountry=@idcountry";
MySqlCommand cmd = new MySqlCommand(strQuery);
//string oldFileName = gvImages.Rows[e.RowIndex].Cells[2].Text;
if (File.Exists(Server.MapPath("images/") + fileName))
{
lblFail.Visible = true;
lblFail.Text = "Ya existe esta entrada.";
TextBox1.Text = "";
}
else
{
//File.Delete(Server.MapPath("images/") + oldFileName);
//guarda archivos al disco
file.SaveAs(Server.MapPath("images/" + fileName));
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Relation", (gvImages.Rows[e.RowIndex].FindControl("txtRel") as TextBox).Text.Trim());
cmd.Parameters.AddWithValue("@FileName", fileName);
cmd.Parameters.AddWithValue("@FilePath", "images/" + fileName);
cmd.Parameters.AddWithValue("@idcountry", Convert.ToInt32(gvImages.DataKeys[e.RowIndex].Value.ToString()));
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
gvImages.EditIndex = -1;
}
catch (Exception ex)
{
lblFail.Visible = true;
lblFail.Text = ex.Message;
}
finally
{
con.Close();
con.Dispose();
}
this.MostrarImagen();
lblSuccess.Visible = true;
lblSuccess.Text = "Exito al editar.";
}
}
fontend(只是gridview)
<asp:Label runat="server" ID="lblSuccess" Text="" ForeColor="Green" Visible="false"></asp:Label>
<asp:Label runat="server" ID="lblFail" Text="" ForeColor="Red" Visible="false"></asp:Label>
<asp:GridView EmptyDataText="No hay registros en la base de datos!" ID="gvImages" runat="server" DataKeyNames="idcountry" OnRowCommand="gvImages_RowCommand" AutoGenerateColumns="false" OnRowDataBound="gvImages_RowDataBound" Height="300px" OnRowEditing="gvImages_RowEditing" OnRowCancelingEdit="gvImages_RowCancelingEdit" OnRowUpdating="gvImages_RowUpdating">
<Columns>
<asp:BoundField DataField="idcountry" HeaderText="ID" ReadOnly="true" />
<asp:TemplateField HeaderText="Relacion">
<ItemTemplate>
<asp:Label Text='<%# Eval("Relation") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtRel" Text='<%# Eval("Relation") %>'></asp:TextBox> />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nombre">
<ItemTemplate>
<asp:Label ID="lblNombre2" Text='<%# Eval("FileName") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FilePath") %>' Height="300" Width="300" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton CausesValidation="false" CssClass="btnOPTIONS" runat="server" ImageUrl="~/imgBTN/edit.png" Width="30px" Height="30px" ToolTip="Edit" CommandName="Edit" />
<asp:ImageButton CausesValidation="false" runat="server" ImageUrl="~/imgBTN/delete.png" Width="30px" Height="30px" ToolTip="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton CausesValidation="false" CssClass="btnOPTIONS" runat="server" ImageUrl="~/imgBTN/save.png" Width="30px" Height="30px" ToolTip="Update" CommandName="Update" />
<asp:ImageButton CausesValidation="false" runat="server" ImageUrl="~/imgBTN/cancel.png" Width="30px" Height="30px" ToolTip="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我试过使用这段代码,但没有成功(这在显示的代码中):
//string oldFileName = gvImages.Rows[e.RowIndex].Cells[2].Text;
以及
//File.Delete(Server.MapPath("images/") + oldFileName);
另外,我的删除方法不会从“images”文件夹中删除图像,只是从数据库中删除。
编辑:这是我取消注解这些代码行时发生的情况:
1条答案
按热度按时间nbysray51#
我已经设法解决了这个问题,我所要做的就是将旧文件名改为:
选择编辑事件之前标签中的数据。
还有:
添加:
在连接的末尾。
最后。。。ಥvಥ