File Download from Gridview in asp.net with C#...............................

Unknown | 6:29 AM |

Downloads.aspx

<asp:GridView ID="gvDownloads"  runat="server" AutoGenerateColumns="False">
            <Columns>
 <asp:TemplateField HeaderText="Brochures" HeaderStyle-BackColor="Gainsboro">
            <ItemTemplate>
         
             <asp:Label ID="lblfile" width="400px"  Text='<%#DataBinder.Eval(Container.DataItem,"filename") %>' runat="server"></asp:Label>
                         
                              <asp:HyperLink ID="HyperLink1"   runat="server"  NavigateUrl='<%# Eval("id", "~/Download.aspx?id={0}") %>'
                              Text="Download"></asp:HyperLink>
                         
              <asp:HyperLink ID="lnkView" runat="server"    NavigateUrl='<%# Eval("id", "~/View.aspx?id={0}") %>' Text="View"></asp:HyperLink>

            </ItemTemplate>
          </asp:TemplateField>
            </Columns>
            </asp:GridView>


Downloads.aspx.cs





  public void display()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select * from downloads", con);
        da.Fill(ds);
        gvDownloads.DataSource = ds;
        gvDownloads.DataBind();
        con.Close();
    }


Download.aspx.cs



using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Web.Mail;
using System.IO;

public partial class Download : System.Web.UI.Page
{
    public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SLMFConnection"].ToString());
    public string conStr = ConfigurationManager.ConnectionStrings["SLMFConnection"].ToString();
    SqlCommand cmd;
    int cid;
 
    byte[] b;
    protected void Page_Load(object sender, EventArgs e)
    {
        int docId = Convert.ToInt32(Request.QueryString["id"]);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select * from downloads where id = @id";
        cmd.Parameters.Add("@id", SqlDbType.Int);
        cmd.Parameters[0].Value = docId;
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            b = ((byte[])(dr["contentfile"]));
            Response.Clear();
            Response.ContentType = dr["contenttype"].ToString();
            Response.AppendHeader("Content-Disposition", ("attachment; filename=\""
                       + (dr["filename"] + "\"")));
            Response.BinaryWrite(b);
        }
        con.Close();
    }
}

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....