Solved

How can I fetch docx or pdf file from MS SQL column for auto email attachment?

Posted on 2014-07-17
5
655 Views
Last Modified: 2014-08-05
Dear Experts,

I'm using ASPNETEmail and I'm looking how to automatically fetch docx or pdf file with specific filename from MS SQL database and attach to an email.

The best solution would be highly appreciated,


Thanks.
0
Comment
Question by:JimiJ13
[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
  • 3
  • 2
5 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 40204928
This depends on the way how the file is stored on the MS SQL Server. Anyway it is easy - simply select the file by appropriate SQL command as any other value then save the retrieved value (yes, it can several megs but that's nothing for today's computers) to a temporary file and then attach this file to the e-mail message as any other file.

Finally you should delete the temp file.

Everything should be done at the ASP.NET server side.
0
 

Author Comment

by:JimiJ13
ID: 40206124
Hi pcelba,

Thanks for the affirmative idea.

The docx or pdf are stored tblName.DocImage (image) with filename in  tblName.DocFileName (varchar (50)) and PK at tblName.DocID (varchar(40)).

Can you give me a working code to retrieve the DocImage based on PK and save to a temp file?


Thanks.
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 40206134
Let suppose you are using C#...

The most important part is the SQL Select command:
SELECT DocImage FROM tblName WHERE DocID = <YourPKvalue>

You just have to provide the correct PK value and implement the SQL command to C#.

To retrieve image data from SQL Server use following code:
http://www.codeproject.com/Articles/354639/Storing-and-Retrieving-Images-from-SQL-Server-Us
You just have to adapt it for e-mail attachment.

To save the retrieved image into a file you may also use following code:
http://www.global-webnet.net/blogengine/post/2008/09/23/Extension-Methods-(C-30)-FileToStr-and-StrToFile-extensions.aspx

And then simply attach the stored file into the e-mail.

You may also look at this code which use File stream data from SQL Server: http://www.c-sharpcorner.com/uploadfile/e628d9/inserting-retrieving-images-from-sql-server-database-without-using-stored-procedures/

And this code is similar to the first one: http://www.codeproject.com/Articles/10861/Storing-and-Retrieving-Images-from-SQL-Server-usin
0
 

Author Comment

by:JimiJ13
ID: 40210976
pcelba,

I will try it and let you know.

Thanks.
0
 

Author Closing Comment

by:JimiJ13
ID: 40241965
Great sources provided to help me come up with a solution.
0

Featured Post

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!

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Invest in your employees with these five simple steps to improve employee engagement and retention.
Viewers will learn how to use the Hootsuite Dashboard.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

732 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