0

I have two separate asp:FileUpload controls under a single asp form like this:

https://i.stack.imgur.com/KJBU9.png

I have written two different functions to save two different files onto a local directory

protected void profileImageUpload()
    {

            if (profile_image_input.HasFile)
            {
            string imagePath = "/Images/" + Session["name"]+"_profile_image"+ System.IO.Path.GetExtension(profile_image_input.FileName);
            profile_image_input.SaveAs(Server.MapPath(imagePath));

            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update uni_login set profile_image_path =" + "'" + imagePath + "'" + "where name =" + "'" + Session["name"] + "';";
            cmd.ExecuteNonQuery();
            con.Close();

            Response.Redirect(Request.RawUrl);
            }
    }

    protected void campusImageUpload()
    {

        if (campus_image_input.HasFile)
        {
            string imagePath = "/Images/" + Session["name"] + "_campus_image" + System.IO.Path.GetExtension(campus_image_input.FileName);
            campus_image_input.SaveAs(Server.MapPath(imagePath));

            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update uni_login set campus_image_path =" + "'" + imagePath + "'" + "where name =" + "'" + Session["name"] + "';";
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

and when I click the "Update Profile" button as seen on the image above, both of the functions get called

protected void updateProfileClick(object sender, EventArgs e)
    {
        profileImageUpload();
        campusImageUpload();
        Response.Redirect(Request.RawUrl);
    }

and onclick:

<asp:Button ID="update_profile_button" Text="Update Profile" OnClick="updateProfileClick" runat="server" CssClass="btn btn-primary"/>

but the problem here is that when the page is loaded and I try to upload two files, that is when i populate both the fileuploads with a file, only the file from first fileupload control gets saved to local directory but when i leave the first fileupload control blank and upload a file on the second fileupload control it gets saved. so the main problem here is that only one file from two of these fileuploads actually gets saved onto the local directory. This seems to be a unique problem I couldn't find solution to this anywhere so any help would be appreciated.

  • `cmd.CommandText = "update uni_login set campus_image_path =" + "'" + imagePath + "'" + "where name =" + "'" + Session["name"] + "';";` As a matter of urgency you need to **stop doing that**. https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection – mjwills May 03 '21 at 08:13
  • @mjwills don't worry about it I'm doing this as a university project. I'm still slowly learning. I will definitely consider security at a later stage as for now, no real data will be used. Thank you for the advice. – Seyed Shaheen May 03 '21 at 08:25
  • 1
    `I will definitely consider security at a later stage` Think about it now. Learn good patterns now, before it is too late. – mjwills May 03 '21 at 08:42

1 Answers1

1

Because you already Response.Redirect(Request.RawUrl) in your first function call. Remove it should solved your problem.

Response.Redirect(string url, bool endResponse) by default the EndResponse parameter is set to true, which will terminate the execution of the current page and the code written after it will not be visit.

You may set EndResponse to false to prevent it from terminating the execution if you do not wish to remove it from that function.

Double E
  • 61
  • 5
  • 1
    I totally missed that line each time I went through the code trying to figure out what's wrong, it's almost like it was never there. Thanks for the quick response and info. Cheers – Seyed Shaheen May 03 '21 at 08:37