Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Here the requirement.
A user can attach multiple files(may be.doc, .pdf whatever), then the file has to be stored in the database when he clicks a submit button. these files( the content of the file) which the user attaches has to be displayed in the data grid view. I am able to get one file attached and viewed in the data grid view, but because the sql table has unique ID column it is able to store and retrieve only one file. I know I have to create a secondary table to implement the multiple attachment logic, but cannot get the connectivity and the code for it.

What I have tried:

// Open file to read using file path
FileStream FS = new FileStream(textBox1.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);

// Add filestream to binary reader
BinaryReader BR= new BinaryReader(FS);

// get total byte length of the file
long allbytes = new FileInfo(textBox1.Text).Length;

// read entire file into buffer
FileBytes = BR.ReadBytes((Int32)allbytes);

// close all instances
FS.Close();
FS.Dispose();
BR.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error during File Read " + ex.ToString());
}
Posted
Updated 20-Oct-16 17:20pm
Comments
gggustafson 20-Oct-16 18:24pm    
Where are you storing the names of the files that the user wishes to attach?

1 solution

The assumption is that you attach the files to something. Therefore you need a container record to attach the uploads to. So yes you need to create a second table (ContainerTable)

ContainerTable
ContainerTableID (primary key = 123)

Attachment
AttachmentID (primary key = 1)
ContainerTableID (foriegn key to the ContainerTable = 123)
 
Share this answer
 
Comments
Member 12805826 21-Oct-16 10:28am    
This a windows form and this form has an
1. where a user can attach multiple files (any type of file) and these files attached has to go to the Table A and have to be saved as Attachment1, attachments2,--(we can limit the number of attachments to 5 with different extensions) in different columns respectively for the same ID)
example: if ID from Table A is 1234. I have to get the attachments in different columns like the
Attachment 1 ID – 1234
Attachment 2 ID -1234 in different columns
And these files has to be displayed in the data grid view
I am able to get one file working the correct way as I wanted I can even see the content of the files in the data grid view.
Problem:
I am not able to get the same ID for all the files attached in the secondary table because the primary table ID column is a unique identifier.
II need to write a sql query to get the different attachments with same ID like this
Attachment 1 ID – 1234
Attachment 2 ID -1234

Thanks In advance
Mycroft Holmes 21-Oct-16 19:14pm    
Your design is incorrect, your attachment table should only have information on one file per record. There should be 5 records for 5 attachments with the PK of 123

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900