Last post Jul 27, 2009 11:17 AM by PeteNet
Jul 25, 2009 07:48 PM|PDTUM|LINK
I am using the following code to retrieve the file name of a pdf document stored in a database. Once I have the string, I have the PDF's stored in a folder under App_Data. I need it to load on the clients computer using their internal PDF reader (whatever that
might be....usually Adobe Reader). I know that all I need to do is to point to the Pdf for it to open on the client side, but since it is different each time depending on the line that was selected from the grid, how do I pass the value? Or is there a better
way to do this?
In C# desktop application, I accomplish this as follows:
System.Diagnostics.Process.Start(Application.StartupPath + @"\PDFs\" + pdf); (pdf is the string returned from the grid).
Obviously, this will not work in ASP.net. All that should be necessary is to be able to create a link when the grid link is clicked on that uses the value of the pdf string variable as returned from the GridView so that when the line is selected from the GridView,
it automatically creates the link and deploys it so that the client's PDF reader will launch and load the pdf. However, I do not know how to accomplish this.
If there another or better way, do let me know and supply me an example using the code sample that I submitted as a starting point. The current code that reurns the correct pdf as a string from the GridView is as follows and works perfectly:
//Define the row and cell
int theRow = Convert.ToInt32(e.CommandArgument);
int theCell = 6;
//Get the Row information
GridViewRow gvr = GridViewResults.Rows[theRow];
//Get the information from the cell
this.thePdf = gvr.Cells[theCell].Text;
//Call Clients PDF reader via a Hyperlink ?? OR ??
<asp:GridView ID="GridViewResults" runat="server" CssClass="gridViewSpecs"
HeaderStyle-Height="30px" Height="100px" Width="770px" BackColor="White"
<RowStyle Height="25px" />
<HeaderStyle BackColor="#CCCCCC" />
<asp:CommandField HeaderImageUrl="~/Graphics/adobe2.PNG" ShowSelectButton="True" />
Thank You, Pat
Jul 26, 2009 04:04 AM|PeteNet|LINK
don't use app_data, create a separate folder, say 'PDFs'
use a LinkButton within a TemplateField in your GridView:
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandArgument = '<%#Eval("<filename>")%>' OnClick="OpenPDF">OpenPDF</asp:LinkButton>
in the codebehind write the event handler OpenPDF:
protected void OpenPDF(object sender, EventArgs e)
LinkButton lnk = (LinkButton)sender;
if (lnk != null)
Response.AddHeader("content-disposition", "attachment; filename=" + lnk.CommandArgument + ".pdf");
Response.WriteFile(Server.MapPath("~/PDFs/" + lnk.CommandArgument.ToString() + ".pdf" ));
commandArgument will hold the filename (from the database, for the current row in the grid)...depending on whether you have the extension stored along with the filename you may want to append or remove ".pdf" in the code (lnk.CommandArgument + ".pdf")
replace <filename> with the actual value of your database column name that holds the file name
if you use VB.NET you can convert over here: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Jul 26, 2009 08:30 AM|JawadHasan|LINK
Well, i have made such thing using Visual Basic.
and if you want not to open file in separate window just remove (Target="_blank")
also dont forget to select the field in your sqlDataSource. e.g.
SELECTCommand="SELECT CDocs.DocID, CDocs.CDocFile, CDocs.PCode, CDocs.DocumentTitle, CDocs.Thumbnail, CDocs.ClientRef,
CDocs.ACESRef, CClients.ClientID FROM CDocs LEFT OUTER JOIN CProjects ON CDocs.PCode = CProjects.PCode LEFT OUTER JOIN CClients ON CProjects.ClientID = CClients.ClientID ORDER BY CDocs.DocID DESC "
ConnectionString="<%$ ConnectionStrings:MatServicesConnectionString %>">
Jul 26, 2009 06:33 PM|PDTUM|LINK
Thank you SO much for your solution....Concise and absolutely perfect. I was amazed at how few people were even willing to take a stab at it. I have marked your repsonse as the correct solution and I sincerely hope that you will look for more of my "issues"
here at ASP.Net (and Code Project) as I painfully transition from C# to ASP.Net.
Jul 26, 2009 07:08 PM|PeteNet|LINK
you are SO welcome for the response!
I'm really glad that I could help. ...yeah, codeproject.com is a good resource.
p.s. I think you marked at the thread level but forgot to mark the specific post as answered?? or is it me?
thanks, and hope your transition is smooth!
Jul 27, 2009 10:48 AM|PDTUM|LINK
I have been trying (desperately) to mark your answer as correct, however, it does not seem to work. I have tried it with all three levels in the thread level resolution menu, and the results are the same. I am going to write to the Sys Admin about it,
but in the meantime, if you have an explanation, please forward it to me so I can get you your due credit for the great solution. As you can see from my stats, although I have been programming for a while, I am new to this site, so I am sorry for my inability
to get you proper credit, however, rest assured that I will not stop until it has been successfully resolved; It is well deserved. Thanks again for your help....Pat
Jul 27, 2009 11:17 AM|PeteNet|LINK
I do believe you. If you have already tried, don't frustrate yourself about it...yes, you can write to the admin...I think you can also click on the
alert moderator link and any of the moderators around/active would be able to mark it and offer an explanation OR you can also write to any of the MSFTs (http://forums.asp.net/24.aspx -botton right corner) and they could mark it
you're welcome anytime....and thanks again!