Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2010, 06:13 AM
Authorized User
 
Join Date: Feb 2006
Posts: 63
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Hannibal
Default Retrieve word document from sql server 2005 using c#

Hi,
I am using ASP.NET 2.0 and c#.

I am uploading a word document into sql server 2005. I am converting the document to a base64string and then passing the value to database. I am able to upload the document. But I am not able to retrieve the document back.
The below code is used for uploading document:
Code:
   if (DocUpload.PostedFile != null || (!String.IsNullOrEmpty(DocUpload.PostedFile.FileName)) || DocUpload.PostedFile.InputStream != null)
                {
                    byte[] imageBytes = new byte[DocUpload.PostedFile.InputStream.Length + 1];
                    DocUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
 
                    string DocFileName = DocUpload.PostedFile.FileName.ToString();
                    string DocContenttype = DocUpload.PostedFile.ContentType;
                    int DocLength =   DocUpload.PostedFile.ContentLength;
 
                    // Convert the binary input into Base64 UUEncoded output.
           string base64String;
                    base64String = System.Convert.ToBase64String(imageBytes, 0, imageBytes.Length);
 
                    InsertCommand.Parameters.Add(new SqlParameter("@MyDoc", SqlDbType.NText));
                    InsertCommand.Parameters["@MyDoc"].Value = base64String.ToString();
                }
I am trying to retrieve the document using the below code:
Code:
  protected void Button1_Click(object sender, EventArgs e)
    {
        string strQuery = "select Doc_Name,Doc_ContentType,My_DocData from tbl_tablename where ID=@ID'";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = 11;
        DataTable dt = GetData(cmd);
        if (dt != null)
        {
            download(dt);
        }  
    }
 private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;    
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }
 private void download(DataTable dt)
    {
        byte[] bytes = ConvertStringToByteArray(dt.Rows[0]["My_DocData"].ToString());
 
       Response.Clear();
       Response.Charset = "utf-8";
       Response.AddHeader("Accept-Header", bytes.Length.ToString());     
       Response.ContentType = dt.Rows[0]["Doc_ContentType"].ToString(); 
       Response.ContentEncoding = System.Text.Encoding.UTF8;
       Response.AddHeader("Accept-Ranges", "bytes");
       Response.Buffer = true;
       Response.AddHeader("Expires", "0");
       Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
       Response.AddHeader("Pragma", "public");
       Response.AddHeader("content-Transfer-Encoding", "binary");   
       Response.AddHeader("content-disposition", "attachment;filename="+ dt.Rows[0]["Doc_Name"].ToString() ; 
       Response.BinaryWrite(bytes);
       Response.Flush();
       Response.End();
    } 
 public byte[] ConvertStringToByteArray(string input)
    {
        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
        return enc.GetBytes(input);
    }
But in the word document, which is getting generated, I am getting the string bytes, rather than the exact document.
How to retrieve the word document from database using c# 2.0?

Thank you
 
Old July 24th, 2010, 10:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You may want to take a look here: http://imar.spaanjaars.com/414/stori...with-aspnet-20 The article presents a complete walkthrough on storing files in a database.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Hannibal (July 28th, 2010)
 
Old July 28th, 2010, 05:00 AM
Authorized User
 
Join Date: Feb 2006
Posts: 63
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Hannibal
Default

Hi Imar,
Thank you very much for the reply.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about SQL Server 2005 and SQL Server Express Edition 2005 gfmann BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 May 24th, 2009 09:36 AM
reg conn to sql server 2005 from vb.net 2005.. veda SQL Server 2005 2 July 1st, 2008 12:16 AM
Not able to retrieve data from SQL Server abhishekkashyap27 C# 2005 1 February 11th, 2008 09:19 AM
Editing Server side Word document narendrapawar VB How-To 1 April 1st, 2005 12:10 PM
saving word document in server asp sdeep_us Classic ASP Basics 1 August 7th, 2004 10:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.