0

I have a mysql table called images with MultipleImageID, MultipleImageName, MultipleImageMap and PropertyID (foreign key). The problem I'm having is the actually images seem to duplicate when I upload them but the columns filled with the correct info. Here is an image to better explain.

[![http://i.imgur.com/oT5GETG.png]]

As you can see the image names are all different as each image should be different but the first image selected seems to upload multiple times. Other times the right image will upload which has confused me as to what was causing this. I am also getting no errors in my code.

Here is my c# for the upload.

protected void Insert(object sender, EventArgs e)
{
     string PropertyName = txtName.Text;
     string PropertyFeatures = txtPropFeat.Text;
     string PropertyLocation = txtPropLoc.Text;
     string PropertyInformation = txtPropInfo.Text;
     string PropertyNumBeds = txtNumBeds.Text;
     string PropertyPrice = txtPrice.Text;
     string PropertyType = txtPropType.Text;
     long InsertedID;

     string constr = ConfigurationManager.ConnectionStrings["realestatedbAddConString"].ConnectionString;

     using (MySqlConnection con = new MySqlConnection(constr))
     {
          using (MySqlCommand cmd = new MySqlCommand("INSERT INTO property (PropertyName, PropertyNumBeds, PropertyType, PropertyPrice, PropertyFeatures, PropertyLocation, PropertyInformation, ImageName, ImageMap) VALUES (@PropertyName, @PropertyNumBeds, @PropertyType, @PropertyPrice, @PropertyFeatures, @PropertyLocation, @PropertyInformation, @ImageName, @ImageMap)"))
          {
              using (MySqlDataAdapter sda = new MySqlDataAdapter())
              {
                  cmd.Parameters.AddWithValue("@PropertyName", PropertyName);
                  cmd.Parameters.AddWithValue("@PropertyNumBeds", PropertyNumBeds);
                  cmd.Parameters.AddWithValue("@PropertyPrice", PropertyPrice);
                  cmd.Parameters.AddWithValue("@PropertyType", PropertyType);
                  cmd.Parameters.AddWithValue("@PropertyFeatures", PropertyFeatures);
                  cmd.Parameters.AddWithValue("@PropertyLocation", PropertyLocation);
                  cmd.Parameters.AddWithValue("@PropertyInformation", PropertyInformation);

                  string FileName = Path.GetFileName(MainImageUploada.FileName);
                  MainImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + FileName);

                  cmd.Parameters.AddWithValue("@ImageName", FileName);
                  cmd.Parameters.AddWithValue("@ImageMap", "ImagesUploaded/" + FileName);

                  cmd.Connection = con;
                  con.Open();
                  cmd.ExecuteNonQuery();
                  InsertedID = cmd.LastInsertedId;
                  con.Close();
                }
            }
        }

        if (ImageUploada.HasFiles)
        {
            foreach (var file in ImageUploada.PostedFiles)
            {
                string FileName1 = Path.GetFileName(ImageUploada.FileName);
                ImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + file.FileName);

                using (MySqlConnection con = new MySqlConnection(constr))
                {
                    using (MySqlCommand cmd = new MySqlCommand("INSERT INTO propertyimage(MultipleImageName, MultipleImageMap, PropertyID) VALUES (@MultipleImageName, @MultipleImageMap, @InsertedID); "))
                    {
                        using (MySqlDataAdapter sda = new MySqlDataAdapter())
                        {
                             cmd.Parameters.AddWithValue("@MultipleImageName", file.FileName);
                             cmd.Parameters.AddWithValue("@MultipleImageMap", "ImagesUploaded/" + file.FileName);
                             cmd.Parameters.AddWithValue("@InsertedID", InsertedID);

                             cmd.Connection = con;
                             con.Open(); 
                             cmd.ExecuteNonQuery();
                             con.Close();
                        }
                    }
                }

            }
        }

        txtName.Text = "";
        txtPropFeat.Text = "";
        txtPropInfo.Text = "";
        txtPropLoc.Text = "";
        txtNumBeds.Text = "";
        txtPrice.Text = "";
        txtPropType.Text = "";

        Label1.Visible = true;
        Label1.Text = "Property Added to Database Successfully!";

    }

I am lost to whether its my code, database or the images I am using.

1 Answer 1

1

You're correctly looping over ImageUploada.PostedFiles, but then you're ignoring that and calling the methods on ImageUploada itself, which will only handle the first file.

So if you upload multiple files, all files saved on the server will be copies of the first file.

You need to change the code to handle each file individually:

foreach (var file in ImageUploada.PostedFiles)
{
    string FileName1 = Path.GetFileName(file.FileName);
    file.SaveAs(Server.MapPath("ImagesUploaded/") + file.FileName);

    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.