?
Solved

Downloading documents from sql server

Posted on 2005-03-31
25
Medium Priority
?
219 Views
Last Modified: 2010-04-07
Does anyone know how to download documents using sql server from asp.net? I have uploaded files to the database and now i want to retrieve those files.
0
Comment
Question by:Rpatel108
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 9
  • 2
25 Comments
 
LVL 11

Expert Comment

by:sachiek
ID: 13677374
You should take look at this below page.

Importing Word Documents into SQL Server
http://www.windowsitpro.com/SQLServer/Article/ArticleID/37903/37903.html
0
 

Author Comment

by:Rpatel108
ID: 13681461
Thanks but this is not actually what i'm looking for!

In my application i want to be able to download documents, and view them from asp.net where the documents are held on sql server. i dont want to directly download them from sql server.
0
 
LVL 11

Expert Comment

by:sachiek
ID: 13686286
Can you tell me which version of word you are using?
Because are you aware of this MS-Office 2003 - which can store all it's document as XML documents.
In that case you can simple to do better. And what you are asking for is going to be easier and straightforward.

Sachi
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rpatel108
ID: 13687825
I'm using office 2003.

But the thing is when i upload documents they can be different types; jpg, txt, doc, etc. so basically all i want to do is the reverse of uploading all those i want to be able to download all of those by hardcoding it in my asp.net code.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13691244
i hope u have the content type stored in the table when u uploaded the documents .. look at this example which explains to upload, delete, retrive documents from SQL server using ASP.net ..
http://www.dotnetspider.com/technology/kbpages/943.aspx
0
 

Author Comment

by:Rpatel108
ID: 13691254
Thanks i will take a look. content type meaning what type of document it is - yeah i have a column for that in my database
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13691263
could u also tell me how u have stored the docs .. in binary format .. right?
0
 

Author Comment

by:Rpatel108
ID: 13691266
yup
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13691293
use this to retrive the document  ..

imports system.data.sqlclient

        Dim lobjConnection As SqlConnection
        Dim lobjCommand As SqlCommand
        lobjConnection = New SqlConnection("urConnectionString")
        lobjConnection.Open()
        lobjCommand = New SqlCommand("Select txt_ContentType, documentFieldName from Table", lobjConnection)
        lobjCommand.CommandType = CommandType.CommandText
        lobjCommand.CommandTimeout = 2000
        Dim ldtrPictures As SqlDataReader = lobjCommand.ExecuteReader()
        Do While (ldtrPictures.Read())
            Response.ContentType = ldtrPictures.Item("txt_ContentType")
            Response.BinaryWrite(ldtrPictures.Item("documentFieldName"))
        Loop

        If Not lobjConnection Is Nothing Then
            If lobjConnection.State = ConnectionState.Open Then
                lobjConnection.Close()
            End If
        End If
        ldtrPictures.Close()
        ldtrPictures = Nothing
        lobjCommand = Nothing
        lobjConnection = Nothing
0
 

Author Comment

by:Rpatel108
ID: 13691349
actually i just checked i've stored them in an image format.

How can i link this code to a datagrid?

Will this code allow the user to save the document on their hardrive? like when you download something it promts you to save it or run it.
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 2000 total points
ID: 13691378
>>actually i just checked i've stored them in an image format.
no problem .. it is binary

>>How can i link this code to a datagrid?
u have a image control in datagrid and u want to show the images within it? if s, u will have to write the code i provided in the load event of a seperate page .. the code is shown below .. i have used a datalist .. u can use a datagrid instead

>>Will this code allow the user to save the document on their hardrive? like when you download something it promts you to save it or run it.
the prompt is based on the settings of the file type in the explorer .. and u cannot control when the popup has to appear .. u can change the settings (but not programatically) .. but then i assume as this been done asp.net the client can be any computer and u will not like to change the settings of the client computer

--- code .
the page where u will have the datagrid ... .aspx
<asp:datalist id="dltImages" runat="server" RepeatColumns="2" ShowFooter="False" ShowHeader="False"
Width="100%">
<AlternatingItemStyle BackColor="#FDFDFD"></AlternatingItemStyle>
<ItemTemplate>
      <TABLE width="100%">
            <TR>
                  <TD align="center">
                        <asp:Image id=imgPicture runat="server" ImageUrl='<%# GetPicture(DataBinder.Eval(Container.DataItem, "id_PropertyPicture")) %>'>
                        </asp:Image></TD>
            </TR>
      </TABLE>
</ItemTemplate>
</asp:datalist>

same .. code-behind .. .vb
    Function GetPicture(ByVal pstrId As String) As String
        If Len(Trim(pstrId)) > 0 Then
            Return ("GetPropertyPicture.aspx?ID=" & pstrId)
        End If
    End Function


GetPropertyPicture.aspx is the new page which will have the code which i had provided in the load event .. it expects the Id as a querystring for which it has fetch the image ..
0
 

Author Comment

by:Rpatel108
ID: 13691399
i get an error: commandtext is not a member of system.data.commandtype
0
 

Author Comment

by:Rpatel108
ID: 13691778
Thank you ever so much it worked, you have really really helped me alot! I couldnt thank you enough. :)
0
 

Author Comment

by:Rpatel108
ID: 13691788
ah i have one problem though, when the document is saved it appears in ASP.net server page. how can i get to its original form? like if its txt, or .doc etc?
0
 

Author Comment

by:Rpatel108
ID: 13691798
It seems to be downloading the actual code of the page which has the load event! which in this case is DocumentData
0
 

Author Comment

by:Rpatel108
ID: 13691804
sorry i meant  GetPropertyPicture.aspx page


i think its something to do with returning bit in the Function GetPicture
("GetPropertyPicture.aspx?ID=" & pstrId)

pstrId - is this meant to be the id of the document?
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13691809
thx for the points !!

>>It seems to be downloading the actual code of the page which has the load event!
what do u mean? does it show the content in binary format?
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13691820
>>pstrId - is this meant to be the id of the document?
s, it is supposed to be the unique id of the table using which u will be able to fetch the image.

assuming ur table structure is
id_PropertyPicture - int - Unique Id
MyImage - image - Binary data
txt_ContentType - Varchar(50) - Content type - eg. application/jpeg etc

<asp:Image id=imgPicture runat="server" ImageUrl='<%# GetPicture(DataBinder.Eval(Container.DataItem, "id_PropertyPicture")) %>'>

u will notice the code of the grid has the imageURL bound to id_PropertyPicture as shown above .. so what happens is when the grid is build, each row will call the function GetPicture which will call the GetPropertyPicture.aspx with the id as the querystring ...
0
 

Author Comment

by:Rpatel108
ID: 13692431
>>>It seems to be downloading the actual code of the page which has the load event!
>>what do u mean? does it show the content in binary format?

i get the actual code written in this file GetPropertyPicture.aspx, not in binary but asp.net code.

basically where is says: GetPropertyPicture.aspx, in ("GetPropertyPicture.aspx?ID=" & pstrId) that is where i think it is returning the page.

I'm not actually using the datalist, or the code that u gave me for the datalist, as it didn't work. but what i did is in my datagrid in the property builder i put this in newdownload.aspx?Document_ID={0} the URL format string. i thought this would bring up the document id of the selected file and return it in the newdownload.aspx page.


in my datagrid i am displaying all the uploaded files, and a hyperlink as the name of the file which when clicked on allows you to download the file that has been clicked on but its giving me the code for the file GetPropertyPicture.aspx.

 so maybe i should put this in my hyperlink URL? :'<%# GetPicture(DataBinder.Eval(Container.DataItem, "id_PropertyPicture")) %>'>

what do you think?
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13695203
what u have done will not work .. if u have a look at the code i have given, u will notice that i have added a image control and the image has the url as the the page.aspx .. what happens is for each row, the code in the page gets executed which returns back the image and which automatically gets assigned to the image control ..
0
 

Author Comment

by:Rpatel108
ID: 13696082
how can i get it so that the file is linked to a hyperlink rather than the image? i dont really want to use an image control.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13696104
u mean show a link to a image?
0
 

Author Comment

by:Rpatel108
ID: 13696116
what i have got at the moment is a datagrid, with the name of the file in the form of a hyperlink, and whe the hyperlink is clicked it then downloads that file. thats what i want.
0
 

Author Comment

by:Rpatel108
ID: 13700351
its ok i got it to work
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13705788
good :-) ...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question