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.
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.
![[![http://i.imgur.com/oT5GETG.png]]](https://www.lemona.fr/i.sstatic.net/8gy0H.png)