Upload PowerPoint and Download from Gridview

Raj.Trivedi
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 3806 red flag

In this article we will be looking into how uploading the powerpoint and then binding its link to GridView to download can be achieved.

Introduction


Recently we were working on the project where we needed to upload the PowerPoint file and save the path to database and then bind the path to the link button for download. Once the user clicks on the link it will open the MS Power Point directly.

Objective

Uploading the PowerPoint and then binding its link to GridView


Using the code


We will take one File Uploader and one Upload Button.

With the Help of the File Uploader, we will select Power Point Files and with the Upload button we will save the Power point file in our application folder and save the path in the database.

Then we will bind the name of the Power Point File and the Path of the File to the grid view.

In the Gridview there will be 3 Columns i.e ID, Power Point File Name and the Path respectively

The Path Column is a Template Field and in that we have placed a link button to download the PPT.

The most important thing we will have to add these references 

using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using ppt = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;

On your solution Folder right Click it - > Add ASP_NET Folder -> Bin -> Then Again right Click Bin Folder -> Add Reference -> Go to .NET Tab Search for above dll's and add it.

In this the last snippet helps to start Power Point Process and Open the File that you have clicked in asp.net application


//Table Script
CREATE TABLE [dbo].[PowerPointData](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[PowerPointName] [varchar](20) NULL,
	[PowerPoint] [varchar](max) NULL
) ON [PRIMARY]

GO
// Stored Proc for Inserting Powerpoint 
create proc [dbo].[InsertPowerPoint]
(
@PowerPointName varchar(20),
@PowerPointPath varchar(max)
)
as
begin
insert into PowerPointData(PowerPointName,PowerPoint)values
(
	@PowerPointName,
	@PowerPointPath
)
end
Stored Proc for Getting data to bind on grid
create proc [dbo].[GetDataPPT]
as
begin
select * from PowerPointData
end

// PowerPoint.aspx	
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PlayPowerpoint.aspx.cs" Inherits="PlayPowerpoint" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="xfuppt" runat="server" /><br />
    <br />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" />
    <br />
    <asp:Literal ID="xErr" Visible ="false" runat="server"></asp:Literal>
    </div>
    <div>
        <asp:GridView ID="xgvDetails" runat="server" AutoGenerateColumns="False" 
            onrowcommand="xgvDetails_RowCommand">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" Visible="False" />
                <asp:BoundField DataField="PowerPointName" HeaderText="File" />
                <asp:TemplateField HeaderText="View">
                    <ItemTemplate>
                        <asp:LinkButton ID="xlnkView" CommandName="View" Text="View" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"PowerPointName") %>' runat="server">LinkButton</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    </div>

    
    </form>
    
</body>
</html>
// Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using App.Data;
using System.Data;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using ppt = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Data.SqlClient;

public partial class PlayPowerpoint : System.Web.UI.Page
{
    string errdesc = "0";
    string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ToString();
   

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid();
        }

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            
            string filename = Path.GetFileName(xfuppt.PostedFile.FileName); // extracting FileName
            xfuppt.SaveAs(Server.MapPath("~/Powerpoints/" + filename));
            string pptpath = "~/Powerpoints/" + filename; extracting path
            SqlConnection con = new SqlConnection(strconn);
            con.Open();
            SqlCommand cmd = new SqlCommand("InsertPowerPoint",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PowerPointName",SqlDbType.VarChar).Value = filename;
            cmd.Parameters.AddWithValue("@PowerPointPath",SqlDbType.VarChar).Value = pptpath;
            cmd.ExecuteNonQuery();
            
            xErr.Text = "Uploaded Successfully";
            xErr.Visible = true;
            FillGrid();
        }
        catch (Exception ex)
        {
            errdesc = ex.Message;
            xErr.Text = errdesc;

        }
     }

    private void FillGrid()
    {
        try
        {
           
            SqlConnection con = new SqlConnection(strconn);
            con.Open();
            SqlCommand cmd = new SqlCommand("GetDataPPT", con);
            DataSet dsgrid = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dsgrid);
           
           
            xgvDetails.DataSource= dsgrid;
            xgvDetails.DataBind();
        }
        catch(Exception ex)
        {
            errdesc = ex.Message;
            
        }
    }
    protected void xgvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "View":
                System.Diagnostics.Process.Start(Server.MapPath("~/Powerpoints/") + e.CommandArgument.ToString()); // syntax to open the PPT File
                break;
        }
    }
}




Conclusion

I hope this article with help any member with similar requirement.
Keep Sharing.



Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Posted by: B4love2013 on: 3/2/2013 | Points: 25
Hello my Dear.
I guess you will not surprise to receive my mail? i saw your profile on [www.dotnetfunda.com]and it sound well.
My name is Benita,Single lady. I am here looking for a good friend.i believe you know that true and trusted friends are worth searching for, i will be happy if you accept my friend request as i am willing to tell you more about myself and to send you my photos, please if you will be willing to accept my request reply me directly to my mail address (benitadesm@yahoo.co.uk) so that i will introduce myself to you,i am waiting for your reply soon.
Remember the distance or color does not matter but love and concern matters a lot in life
Thanks.
(benitadesm@yahoo.co.uK)
Benita.

Login to post response

Comment using Facebook(Author doesn't get notification)