Click here to Skip to main content
15,794,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, I'm using the following code to insert files into my DB:

                <asp:FileUpload ID="FileUpload" runat="server" />
                 <br />
                <asp:Button ID="btnUpload" runat="server" Text="Upload"
                    OnClick="btnUpload_Click" />
                <asp:Label ID="lblMessage" runat="server" Text=""

with the background code:

protected void btnUpload_Click(object sender, EventArgs e)
            // Read the file and convert it to Byte Array
            string filePath = FileUpload.PostedFile.FileName;
            string filename = Path.GetFileName(filePath);
            string ext = Path.GetExtension(filename);
            string contenttype = String.Empty;

            //Set the contenttype based on File Extension
            switch (ext)
                case ".doc":
                    contenttype = "application/";
                case ".docx":
                    contenttype = "application/";
                case ".xls":
                    contenttype = "application/";
                case ".xlsx":
                    contenttype = "application/";
                case ".jpg":
                    contenttype = "image/jpg";
                case ".png":
                    contenttype = "image/png";
                case ".gif":
                    contenttype = "image/gif";
                case ".pdf":
                    contenttype = "application/pdf";
            if (contenttype != String.Empty)

                Stream fs = FileUpload.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);

                //insert the file into database
                string strQuery = "INSERT INTO Files(Name, Content_Type, Data)" +
                   " VALUES (@Name, @ContentType, @Data)";
                SqlCommand cmd = new SqlCommand(strQuery);
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
                cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
                  = contenttype;
                cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "File Uploaded Successfully";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "File format not recognised." +
                  " Upload Image/Word/PDF/Excel formats";

        private Boolean InsertUpdateData(SqlCommand cmd)
            String strConnString = System.Configuration.ConfigurationManager
            SqlConnection con = new SqlConnection(strConnString);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
                return true;
            catch (Exception ex)
                return false;

What happens here is that everytime i click on the upload button the file uploads and saves correctly into the DB, this is working perfectly... the problem is this isn't how I want it to work since this is from a WebForms page and there are many more form fields which I'm inserting to a DB through a different button. What I want is for users to be able to select several files but they only upload and are saved into the DB when all the others are...
I'm using 2 tables one for all the others fields and 1 for the files...

Please give me suggestions on how to do this the way I want it
Sinisa Hajnal 28-Jan-15 6:06am    
You'll have to use one of the multiupload controls then. If you use .NET 4.5, simply set AllowMultiple property to true on the upload control.

If not, you'll have to program it.
varmartins 28-Jan-15 6:56am    
and how do I associate the file table with the form table in a way that all the images uploaded will be connected with the form they were uploaded with?

1 solution

Here is one finished product that you can use:
jQuery multiupload widget[^]
Share this answer

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