Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, I'm trying to display a word document in my asp.net page which had been saved in Access database 2003 as an OleDb Object. If I'm not mistaken the process is the same as retrieving and displaying an image from the database. I've found a snippet of code online for retrieving doing just that and have tried to study it to understand how it works. This is what I understand from looking at the code:

1 Grab the id of the file you want to display from the database using a sql statement
2 Grab the content type of the file you want using a sql statement
3 Read the BLOB value using the ExecuteScalar() method of the SqlCommand class.
4 Cast the return object to byte array and store it in byte array because
executeScalar() returns an object.
5 Stream it back to the HTTP response

The following is the code snippet I'm trying to use:

C#
void  CreateImage(string id)
{
    // Connectoin string is taken from web.config file.
    SqlConnection _con = new SqlConnection(
      System.Configuration.ConfigurationSettings.AppSettings["DB"]);
        
    try
    {
        _con.Open();
        SqlCommand _cmd = _con.CreateCommand();
        _cmd.CommandText = "select logo from" + 
                           " pub_info where pub_id='" + 
                           id + "'";
        byte[] _buf = (byte[])_cmd.ExecuteScalar();
        
        // This is optional
        Response.ContentType = "image/gif";
        
        //stream it back in the HTTP response
        Response.BinaryWrite(_buf);                
                
    }
    catch
    {}
    finally
    {
        _con.Close();
                
    }
}


In the Page_Load(), call CreateImage method.


C#
private void Page_Load(object sender, System.EventArgs e)
{
    if(!IsPostBack)
    {
        CreateImage(Request["id"]);
    }
}

Then use
C#
<img src='<%# "imgs.aspx?id=" + drpIds.SelectedValue %>'>
to display image

Finally call
C#
Page.databind.


My question is how can I modify the step
C#
<img src='<%# "imgs.aspx?id=" + drpIds.SelectedValue %>'>
to display a word doc in a text field. Thanks in advance for your help.
Posted
Updated 15-Aug-12 5:39am
v5

Please have a look at my response below for similar kind of question.

sql server 2008 BLOBs[^]

I have attached sample solution in the link.
 
Share this answer
 
Comments
ASPnoob 15-Aug-12 10:38am    
Your suggestion is irrelevant to my question.
Santhosh Kumar Jayaraman 15-Aug-12 10:40am    
how you are expecting to display a word doc in image control.
ASPnoob 15-Aug-12 11:35am    
Please read my question.
Follow this link
it is discussed there how this can be done and also its alternative.
 
Share this answer
 
Comments
ASPnoob 15-Aug-12 10:39am    
I did not find anything useful in there.
abhijeetgupta1988 15-Aug-12 10:50am    
they have discussed how it can be done if you want to store into DB.
and the alternative was to store the doc in file system and maintaining the link in DB(this is what I always prefer,as owing to the limited capacity of SQL express edition i.e. about 10GB,and also the conversion time in storing and retrieving the BLOB)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900