Solved

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

Posted on 2014-07-17
5
649 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

830 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