Click here to Skip to main content
16,013,919 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
this code take only first image and show only paths for another image
C#
foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles)
        {
            string filename = Path.GetFileName(postedFile.FileName);
            FileUpload2.SaveAs(Server.MapPath(".") + "//im1//" + filename);
            string path2 = "~//im1//" + filename.ToString();
            string contentType = postedFile.ContentType;
            using (Stream fs = postedFile.InputStream)
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                     string constr =ConfigurationManager.ConnectionStrings["newsConnectionString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
string query = "insert into news(image_name,content_type,data)values(@Name, @ContentType, @Data)";
 using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", path2);
                            cmd.Parameters.AddWithValue("@ContentType", contentType);
                            cmd.Parameters.AddWithValue("@Data", bytes);
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                }
}          
Posted
Updated 19-Dec-15 23:50pm
v2
Comments
Kornfeld Eliyahu Peter 20-Dec-15 9:32am    
Did you run in debug to see if the insert actually executed several times?
[no name] 21-Dec-15 1:57am    
Do you want to insert multiple images?

Then you can execute the insert statement multiple times with image data.

If you need something else then describe it.

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