Solved

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

Posted on 2014-07-17
5
643 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
pcelba,

I will try it and let you know.

Thanks.
0
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Specific format 21 140
WMI, model #, retrieving information 10 85
wordlen challenge 3 46
PHP question(s) about order of output 9 39
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
This is about my first experience with programming Arduino.
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now