Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnSave_Click1(object sender, EventArgs e)
{
    List<string> img=new List<string>();
    HttpFileCollection httpFileCollection = Request.Files;
    for (int i = 0; i < httpFileCollection.Count; i++)
    {
        HttpPostedFile httpPostedFile = httpFileCollection[i];
        string fileNameExtension = Path.GetFileName(httpPostedFile.FileName);
        if (httpPostedFile.ContentLength > 0)
        {
            httpPostedFile.SaveAs(Server.MapPath("~/Upload/Testimonial/")+ "\\"+ fileNameExtension);
            img.Add(Server.MapPath("~/Upload/Testimonial/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
        }
        

    }
    if (btnSubmit.Text == "Submit")
    {
        bool a = video.SaveUpdatetestimonial(1, Editor.Content, img.ToArray().Length > 0 ? string.Join(",", img.ToArray()) : Path.GetFileName(FileUpload1.FileName), txtEmbed.Text, TextBox1.Text, 0);
        if (a == true)
        {
            txtEmbed.Text = "";
            TextBox1.Text = "";
            Editor.Content = "";
            lblInfo.Text = "Record Inserted Successfully";
            BindGrid(1);
        }
        else
        {
            lblInfo.Text = "Record not Saved";
        }
    }
    else
    {
        if (btnSubmit.Text == "Update")
        {
            int Sno = Convert.ToInt32(grdImg.Rows[temp].Cells[1].Text);
            bool a = video.SaveUpdatetestimonial(2, Editor.Content, img.ToArray().Length > 0 ? string.Join(",", img.ToArray()) : System.IO.Path.GetFileName(FileUpload1.FileName), txtEmbed.Text, TextBox1.Text,Sno);
            if (a == true)
            {
                txtEmbed.Text = "";
                Editor.Content = "";
                lblInfo.Text = "Record Updated";
                BindGrid(1);
                TextBox1.Text = "";
            }
            else
            {
                lblInfo.Text = "Record Not Updated";
            }
        }
    }
}

what is error in it ? so it is storing only last selecting image. Help me, Thanks in advance
Posted
v2
Comments
Thomas ktg 5-Sep-13 7:43am    
You have given the forloop only to save images. In addition to that you must also take all the image's names to store in the database from the httpFileCollection. But I dont think you are taking it and storing in database. Only one file name is stored once forloop is complicated. You check with the httpFileCollection and add the names in order to store one by one. I hope i make clear to you.

1 solution

For this code, create file name as variable and then add it to the collection. In 2 lines you have used 2 different names coming out of 2 different items
C#
httpPostedFile.SaveAs(Server.MapPath("~/Upload/Testimonial/")+ "\\"+ fileNameExtension);
            img.Add(Server.MapPath("~/Upload/Testimonial/") + Path.GetFileName(FileUpload1.PostedFile.FileName));

First line is taking from httpPostedfFile and another is taking from FileUpload control. So this is a contradiction. The file upload control will always have a single file name so it will be a repeated entry.

Having saparate variables instead of inline code will clear out the code and help with debugging breakpoints as well
 
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