Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

For Download a file I like to use the below code that I found in the net. So I have made a grid view that first column is file name.

Now when I run the application, I have the message "This file does not exist.".

So for checking my code I have added statement Response.Write(filename)<code> after statement <code>string filename = rowItem.Cells[0].ToString(), so after running the application it returns System.Web.UI.WebControls.DataControlFieldCell for the value " filename".

Please help what is the problem.
------------------------------------------------------

C#
protected void lnkPath_OnClick(object sender, EventArgs e)
    {
        GridViewRow datarow = (GridViewRow)(((Control)sender).NamingContainer);
        int i = datarow.RowIndex;
        foreach (GridViewRow rowItem in grdv.Rows)
        {
            if (rowItem.RowIndex == i)
            {                
                string filename = rowItem.Cells[0].ToString();
                if (filename != "")
                {
                    string path = Server.MapPath(filename);
                    System.IO.FileInfo file = new System.IO.FileInfo(path);
                    if (file.Exists)
                    {
                        Response.Clear();
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.WriteFile(file.FullName);
                        Response.End();
                    }
                    else
                    {
                        Response.Write("This file does not exist.");
                    }
                }
            }
        }
    }


----------------------------------------------
ASP.NET
<asp:GridView ID="grdv" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Title" >
<ItemTemplate>
<asp:LinkButton ID="lnkPath" runat="server" Text= '<%# Bind("Coach_First") %>' OnClick="lnkPath_OnClick" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DocsID" >
<ItemTemplate>
<asp:Label ID="lblDocsID" runat="server" Text='<%# Bind("CoachId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProjectID" >
<ItemTemplate>
<asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("Coach_Last") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Webpath" >
<ItemTemplate>
<asp:Label ID="lblWebpath" runat="server" Text='<%# Bind("Coach_Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> 
</Columns>
</asp:GridView>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:conn %>" ID="SqlDataSource1"
runat="server" SelectCommand="SELECT top 5 CoachId, Coach_First, Coach_Last, Coach_Email from tblCoach">
</asp:SqlDataSource>
Posted
Updated 3-Mar-12 18:06pm
v3
Comments
Mahmud Hasan 4-Mar-12 0:24am    
I do not see anything named FileName bound with any column of your gridview. Am i missing anything?

1 solution

The reason why its returning System.Web.UI.WebControls.DataControlFieldCell is because of this line.
C#
string filename = rowItem.Cells[0].ToString();

Try replacing it with
C#
string filename = rowItem.Cells[0].Text;
 
Share this answer
 

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