Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a gridview in which there is a column to 'Remove zip file'. The zip file in question is being added by the user to a folder in the application and the name is stored in the database. Each row of the GridView has a zip file associated with it. Now, when I click on the 'Remove Zip file' button in GridView, I want to delete this zip file. Here is the code I am using.

XML
<asp:GridView ID="gvrecord" runat="server" AutoGenerateColumns="False" BackColor="White"
        BorderStyle="None" BorderWidth="1px" CellPadding="4" onrowcancelingedit="gvrecord_RowCancelingEdit"
           onrowdatabound="gvrecord_RowDataBound" onrowdeleting="gvrecord_RowDeleting"
           onrowediting="gvrecord_RowEditing" onrowupdating="gvrecord_RowUpdating" Height="168px"
                       Width="850px">
            <RowStyle BackColor="White"/>
           <Columns>
                   <asp:TemplateField HeaderText="PROJECT ID" ItemStyle-Font-Size="12px">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text='<%#Bind("ProjectId") %>'></asp:Label>
                   </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="JOB TITLE" ItemStyle-Font-Size="12px">
                   <ItemTemplate>
                       <asp:Label ID="lbljob" runat="server" Text='<%#Bind("jobtitle") %>'></asp:Label>
                   </ItemTemplate>

.
.
.
.
XML
<asp:TemplateField ItemStyle-Font-Size="12px">
                   <ItemTemplate>
                       <asp:LinkButton ID="lnkremovezip" runat="server" OnClick="lnkremovezip_Click">Remove Zip File</asp:LinkButton>
                   </ItemTemplate>
                   </asp:TemplateField>

and here is the code for the lnkremovezip_Click()
C#
protected void lnkremovezip_Click(object sender, EventArgs e)
{
   LinkButton lnkzipremove = (LinkButton)sender;
   GridViewRow row = (GridViewRow)lnkzipremove.NamingContainer;
   Label lblzipfile = (Label)row.FindControl("lblzip");
   string path = "~/ZipFiles/" + lblzipfile.Text;
   FileInfo file = new FileInfo(path);
   if (file.Exists)
   {
       file.Delete();
   }
}


I put breakpoints in the code and found that when the execution gets to the 'if' statement, the condition is false and it doesn't execute the file.Delete().The zip file is present in the /ZipFiles/ folder.

Can somebody please help me figure out where I've gone wrong?
Posted
v2
Comments
sumit_kapadia 18-Jul-13 2:48am    
You can check by copy value from path variable and paste in run Command or windows explorer to check whether it pointing to correct file. alternatively you can try
Server.MapPath(path);
to get exact path in your solution

try this you are missing Server.MapPath

C#
protected void lnkremovezip_Click(object sender, EventArgs e)
{
LinkButton lnkzipremove = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnkzipremove.NamingContainer;
Label lblzipfile = (Label)row.FindControl("lblzip");
string path = "~/ZipFiles/" + lblzipfile.Text;
FileInfo file = new FileInfo(Server.MapPath(path));
if (file.Exists)
{
file.Delete();
}
}
 
Share this answer
 
Hey Guys,
I figured out the problem. Just needed to add Server.MapPath to the path.

C#
FileInfo file=new FileInfo(Server.MapPath(path));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900