Every search that I come up with is wanting to save the results displayed in the datagrid. My page would be a list of all documents stored in the database. Actually what is stored in the database is just a webpath on the server.
My sql table has for columns - DocsID , ProjectID, Title, Webpath
I was looking for a grid that looked something like this:
DocsID ProjectID Title Webpath
Click Me to Download: 1 , 32, My I love SQL.pdf C:/servertarget
Click Me to Download: 2 , 32, Why does my hair fall out?.doc, C:/servertarget
Do I just want to make a datagrid and put a hyperlink on the Webpath field?
Would someone mind giving me a hand in code? I would appreciate it - See DocsID # 2 as a reference :-( b/c I'm quite fond of my hair
The easiest way to do this is to just have a varchar field in SQL that stores the entire file location. Then in your datagrid you can generate a hyperlink column using the stored value.
<P mce_keep="true">Yes, that is actually what I do have going for me. Just not sure how to grab the path and download when the hyperlink is clicked. Maybe it would help if I post my connection string. Which is just about all I've got. I do
have a hyperlink column which, which is displaying my link, but I don't know how to bind it to the webpath column. thanks
</P>
Ram Redy I believe the way you gave me is ado correct? I'm going to use it for now. And VaserZaid, a lot of logic in yours. I'm going to work that in when I can. Again, just can't thank you enought - - and fast too!
Guys I appreciate your help. I was able to get some of this code working to fill my datagrid, display a hyperlink and in one case get an error that a file was not found - even though it was. I fished out something old from one of my co-workers code. It's
into a gridview. I'm populating just fine and am using a hyperlink field to display my link. He was using
DataNavigateUrlFormatString to pass values between pages. I'm trying to tap the db and pull the virtual path out as when the link is clicked the save/ save as dialog box opens. I'm hung up on this. I may have even been
given the answer in previous posts but don't know how to integrate it in. I would certainly appreciate the help to pull a path out. Could you integrate into my code? thanks much!
Swank
Member
11 Points
60 Posts
Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a data...
Apr 08, 2009 02:43 PM|LINK
Every search that I come up with is wanting to save the results displayed in the datagrid. My page would be a list of all documents stored in the database. Actually what is stored in the database is just a webpath on the server.
My sql table has for columns - DocsID , ProjectID, Title, Webpath
I was looking for a grid that looked something like this:
DocsID ProjectID Title Webpath
Click Me to Download: 1 , 32, My I love SQL.pdf C:/servertarget
Click Me to Download: 2 , 32, Why does my hair fall out?.doc, C:/servertarget
Do I just want to make a datagrid and put a hyperlink on the Webpath field?
Would someone mind giving me a hand in code? I would appreciate it - See DocsID # 2 as a reference :-( b/c I'm quite fond of my hair
Thanks much!!
TommyGunn32
Member
117 Points
37 Posts
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 08, 2009 03:43 PM|LINK
The easiest way to do this is to just have a varchar field in SQL that stores the entire file location. Then in your datagrid you can generate a hyperlink column using the stored value.
Ram Reddy Me...
Star
9604 Points
1314 Posts
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 08, 2009 05:04 PM|LINK
You can do like this...
<
asp:GridView ID="grdv" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Title" > <ItemTemplate> <asp:LinkButton ID="lnkPath" runat="server" Text= '<%# Bind("Coach_First") %>' OnClick="lnkPath_OnClick" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="DocsID" > <ItemTemplate> <asp:Label ID="lblDocsID" runat="server" Text='<%# Bind("CoachId") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ProjectID" > <ItemTemplate> <asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("Coach_Last") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Webpath" > <ItemTemplate> <asp:Label ID="lblWebpath" runat="server" Text='<%# Bind("Coach_Email") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:conn %>" ID="SqlDataSource1" runat="server" SelectCommand="SELECT top 5 CoachId, Coach_First, Coach_Last, Coach_Email from tblCoach"> </asp:SqlDataSource>and in the on click of link button
protected void lnkPath_OnClick(object sender, EventArgs e) { GridViewRow datarow = (GridViewRow)(((Control)sender).NamingContainer); int i = datarow.RowIndex; foreach (GridViewRow rowItem in grdv.Rows) { if (rowItem.RowIndex == i) { string filename = rowItem.Cells[0].ToString(); if (filename != "") { string path = Server.MapPath(filename); System.IO.FileInfo file = new System.IO.FileInfo(path); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); } } } } }Let me know if you have any queries.Abhiram Reddy Mekha
Swank
Member
11 Points
60 Posts
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 08, 2009 05:09 PM|LINK
<P mce_keep="true">Yes, that is actually what I do have going for me. Just not sure how to grab the path and download when the hyperlink is clicked. Maybe it would help if I post my connection string. Which is just about all I've got. I do have a hyperlink column which, which is displaying my link, but I don't know how to bind it to the webpath column. thanks
</P>
<form id="Form1" OnInit="PageInit" Runat="server">
<asp:DataGrid ID="grid" OnLoad="GridLoad" Runat="server" >
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="ProjectID" DataTextField="ProjectID" HeaderText="filename"></asp:HyperLinkColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:HyperLink Text="Proj Docs Id" runat="server" >
<%# DataBinder.Eval(Container.DataItem, "WebPath" ) %><br />
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
C#
public class BasePage : Page
{
protected DataGrid grid;
DataView dataView;
public void PageInit(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("server=bucklel\\SQLEXPRESS;Initial Catalog=MSIC_Materials_v2;Integrated Security=True");
string sqlString = "SELECT * FROM ProjectSourceDocs";
SqlDataAdapter adapter = new SqlDataAdapter(sqlString, con);
adapter.Fill(ds, "ProjectSourceDocs");
if (ds.HasErrors) ds.RejectChanges(); else ds.AcceptChanges();
dataView = ds.Tables["ProjectSourceDocs"].DefaultView;
}
public void GridLoad(object sender, EventArgs e)
{
grid.HeaderStyle.Font.Bold = true;
grid.AlternatingItemStyle.BackColor = System.Drawing.Color.LightGray;
grid.DataSource = dataView;
grid.DataBind();
}
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 08, 2009 05:37 PM|LINK
Refer my article
http://www.aspsnippets.com/post/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASPNet.aspx
Contact me
Swank
Member
11 Points
60 Posts
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 08, 2009 06:04 PM|LINK
Man, Ram Redy & VaserZaid, You both are amazing.
Ram Redy I believe the way you gave me is ado correct? I'm going to use it for now. And VaserZaid, a lot of logic in yours. I'm going to work that in when I can. Again, just can't thank you enought - - and fast too!
Swank
Member
11 Points
60 Posts
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 08, 2009 06:05 PM|LINK
All-Star - - good article I'll take a look at that too!
Swank
Member
11 Points
60 Posts
Re: Download a file from sql (doc, txt, xls, pdf) files available in the db to be displayed in a ...
Apr 09, 2009 10:08 PM|LINK
Guys I appreciate your help. I was able to get some of this code working to fill my datagrid, display a hyperlink and in one case get an error that a file was not found - even though it was. I fished out something old from one of my co-workers code. It's into a gridview. I'm populating just fine and am using a hyperlink field to display my link. He was using DataNavigateUrlFormatString to pass values between pages. I'm trying to tap the db and pull the virtual path out as when the link is clicked the save/ save as dialog box opens. I'm hung up on this. I may have even been given the answer in previous posts but don't know how to integrate it in. I would certainly appreciate the help to pull a path out. Could you integrate into my code? thanks much!
<asp:GridView ID="ctlGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="SampleID"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="groove" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" EmptyDataText="No Records Found"> <FooterStyle BackColor="#CCCC99" /> <RowStyle BackColor="#F7F7DE" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <%--DataNavigateUrlFormatString="~/DownloadingPage.aspx?projID={0}"--%> <asp:HyperLinkField DataNavigateUrlFields="SampleID" HeaderText="SampleID:" DataTextField="SampleID" DataNavigateUrlFormatString="{0}"/> <asp:BoundField DataField="Title" HeaderText="Document Title" SortExpression="Title" HtmlEncode="false" /> <asp:BoundField DataField="WebPath" HeaderText="Source Documents" SortExpression="WebPath" HtmlEncode="false" /> </Columns> </asp:GridView>public
partial class LibraryGrid : System.Web.UI.Page{
SqlConnection myconnection;protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack){
this.ctlGridView.DataSource = this.myconnection;}
myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["My Database"].ConnectionString);myconnection.Open();
SqlDataReader myreader = null; String query = buildQuery(this.TextB_100.Text); SqlCommand mycommand = new SqlCommand(query);mycommand.Connection = myconnection;
myreader = mycommand.ExecuteReader();
ctlGridView.DataSource = myreader;
ctlGridView.DataBind();
grdProject.DataSource = myreader;
grdProject.DataBind();
myconnection.Close();
}
protected static String buildQuery(String SampleID){
String query = "SELECT s.SampleID, ssd.Title, ssd.WebPath ";query += "FROM Sample s RIGHT JOIN SampleSourceDocs ssd ON s.SampleID=ssd.SampleID"; if (SampleID.Length > 0) query += " WHERE SampleID LIKE '%" + SampleID + "%'"; return query;}
}