Insert multiple images to database using FileUpload control in ASP.Net

Last Reply on Mar 26, 2015 03:46 AM By Mudassar

Posted on Mar 26, 2015 03:45 AM

hi sir 

i am try this code for multipul image upload in database 

but only one image store in database  at a time 

 

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (file.HasFile == false)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "<script>alert('No File Uploaded.')</script>", false);
        }
        else
        {
            foreach (var files in file.PostedFiles)
            {
                con.Open();
                string filename = Path.GetFileName(files.FileName);

                files.SaveAs(Server.MapPath("/Upload/" + filename));
                int id = 1;
                SqlCommand cmd = new SqlCommand("Insert into tbl_ProductImage values(" + id + ",'" + filename + "')", con);
                cmd.ExecuteNonQuery();
                //con.Close();
            }
        }
    }

design

 

<asp:FileUpload ID="file" runat="server" AllowMultiple="true" /></div>
                <input type="button" id="add_more" class="upload" value="Add More Files"/>
           <%-- <input type="submit" value="Upload File"  name="submit" id="upload" class="upload"/>--%>
                <asp:Button ID="Button1" runat="server" Text="Upload File"  CssClass="upload" OnClick="Button1_Click" />

 

You are viewing reply posted by: Mudassar on Mar 26, 2015 03:46 AM.
Posted on Mar 26, 2015 03:46 AM