I have a table called Messages which has a field called Attachment. The Attachment field uses a data type of "image" and binary data is inserted into that field whenever a file is uploaded from my website.

This is the code to insert the attachment into the database:

string cmdAnnouncement = "INSERT INTO Messages([AnnouncementCategoryId], [Subject], [Body], [CreateDate], [CreateMemberId], [MemberOnly], [Attachment]) VALUES ('" + selCategory.SelectedItem.Value + "', '" + txtSubject.Text + "', '" + txtBody.Text + "', '" + DateTime.Now + "', '" + readID.GetInt32(0) + "', '" + true + "', '" + fileUpload.FileBytes + "')";

SqlCommand cmd = db.executeSQL(cmdAnnouncement);

YES I know I should use parameters but leave me alone about that for now. So, as it seems, the attachment goes into the database fine. But I run into a problem when trying to allow users to download the attachment from the database.

I basically don't know what to do to allow the users to download the attachment. I want a hyperlink to be inserted onto my websites page and when the users click a specific hyperlink, they are directed to a specific attachment and are allowed to download it. Please explain how I can make this.

Firstly you can't insert array of bytes (fileUpload.FileBytes) into a table via sql string. Use @Parameterized query. (See MSDN pages - Parameterized query).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.