locked
Two file inputs on one page. RRS feed

  • Question

  • User895691971 posted

    Hi,

    I am having two forms, one is for post (contains text input, textarea, file input and a submit button) the other one is for just image (file input and submit button). 

    The issue I am falling into is something like:

    Object not set to instance of the object.

    I have tried using 

    <input type="hidden" name="type" value="picture" />

    But its no use I think. The code I am using is something like: 

     if(IsPost) {
            if(req_type == "dp") {
                // change the profile picture
                dp = WebImage.GetImageFromRequest();
                if(dp != null) {
                    var newFileName = "";
                    var imagePath = "";
                    newFileName = Guid.NewGuid().ToString() + "_" +
                    Path.GetFileName(photo.FileName);
                imagePath = @"User_Files\Images\" + newFileName;
                photo.Save(@"~\" + imagePath);
                var insertQuery = "INSERT INTO UserPosts (PostFromId, Time, Message, ImageName, DataType, Profilepicture, PostFromName, Privacy) VALUES (@0, @1, @2, @3, @4, @5, @6, @7)";
                db2.Execute(insertQuery, ID, time, "", newFileName, "4", myDp, myFullName, "3");
                db2.Execute("INSERT INTO Photos(Name, Time, Message, FromId, FromName, FromDp, ProfilePic) VALUES (@0, @1, @2, @3, @4, @5, @6)", newFileName, DateTime.Now, message, WebSecurity.CurrentUserId, myFullName, myDp, 1);
                Response.Redirect(@Href("~/profile", ID));
                }
            } 

    Should not be confused with the variables myDp, myFullName, they are accessed at the back end, I mean at the top of the page! 

    The forms are as: 

    // form one! the one having issues..
    <form method="post" enctype="multipart/form-data">
    <input type="file" accept="Image/*" name="Image" />
    <input type="hidden" name="form-type" value="dp" />
    <input type="submit" value="Set as Profile Picture" />
    </form>
    
    // second one, that's going perfect!
    <form method="post" enctype="multipart/form-data">
    <textarea rows="3" cols="50" name="message" placeholder="Whats going on?"></textarea><br>
    <input type="text" name="tags" placeholder="Hey use ',' to seperate tags." />
    <input type="file" class="picture-uploader" accept="Image/*" name="Image" /><br>
    <select name="privacy">
    <option value="0">Only me</option>
    <option value="1">Friends</option>
    <option value="2" selected>Public</option>
    </select>
    <input class="updater" type="submit" value="Post" />
    </form>

    When I submit the second one, it goes as smooth as it should. But the first form gives me error. 

    Wednesday, August 28, 2013 9:32 AM

Answers

  • User-760709272 posted

    "photo" is probably null.  Use the debugger to find out the values of your variables, but that seems to be the most likely candidate.  How are you populating photo?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 8:17 PM

All replies

  • User-760709272 posted

    You should say what line the error is on, however I'm guessing the problem is that you're not appreciating that the only thing submitted with a form is everything in that form.  When you submit the top form, the controls in the bottom one are not posted so you don't have access to them.

    Wednesday, August 28, 2013 10:02 AM
  • User895691971 posted

    The error has been posted, and its for the Guid line. The first line where the thing should start!

    Wednesday, August 28, 2013 7:57 PM
  • User-760709272 posted

    "photo" is probably null.  Use the debugger to find out the values of your variables, but that seems to be the most likely candidate.  How are you populating photo?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 8:17 PM
  • User895691971 posted

    Silly me!

    The photo is the variable name for the image in the second form. The dp is the variable for the first form. :/ I missed that! 

    Thanks for pointing it out.

    Thursday, August 29, 2013 7:26 AM