Solved

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

Posted on 2014-07-17
5
646 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
  • 3
  • 2
5 Comments
 
LVL 41

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 41

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 62
best free software for ripping cd's 11 120
Transferring files to iPhone 3 55
SQL Insert parts by customer 12 33
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 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