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.