aman sohal

aman sohal

  • 2.1k
  • 3
  • 2k

can any one help me in my c# project??

Aug 23 2010 11:41 PM

i have problem with download from grid view using a link button on gird view ,, i am able to download predefined data by displaying in grid view and then downloading by click ing on link button in grid view column,,but i want my data to be download from database(ms access),,so that when ever i upload new data come available in grid view and then i can download it via link button in grid ,,,here code which i used  for static download ,,,as data is prefined ,,,i need some help ,,,regarding this code ,,,how can i use database and then download into grid view and then download from grid view ,,,i am new to c# aspnet ,, so please help me out,,read my code ,,  u will get idea what i want to achieve,,
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                Width="215px" OnRowCommand="GridView1_RowCommand"
                onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:ImageField DataImageUrlField="ImageUrl" HeaderText="Image">
                    </asp:ImageField>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                CommandName="Download" Text="Download"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </div>
    </form>
</body>
</html>
 
code behind section
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    public class Person
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string ImageUrl
        {
            get;
            set;
        }
        public Person(int Id, string Name, string ImageUrl)
        {
            this.Id = Id;
            this.Name = Name;
            this.ImageUrl = ImageUrl;
        }
    }
    //Binding some sample data to the GridView
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Populating some dummy data. In reality, you should populate these
            //from the database
            IList items = new ArrayList();
            items.Add(new Person(1, "Peter", "~/images/ab.jpg"));
            items.Add(new Person(2, "John", "~/images/cd.jpg"));
            items.Add(new Person(3, "Shubho", "~/images/ef.jpg"));
            GridView1.DataSource = items;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            string ImageUrl = (string)e.CommandArgument;
            string physicalImagePath = Server.MapPath(ImageUrl);
            if (File.Exists(physicalImagePath))
            {
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(physicalImagePath));
                Response.ContentType = "image/jpg";
                Response.TransmitFile(physicalImagePath);
                Response.Flush();
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
            if (link != null)
            {
                link.CommandArgument = ((Person)e.Row.DataItem).ImageUrl.ToString();
            }
        }
    }
}
 
 

Answers (4)