title: How do I preset a file upload that is empty?
body: <p>I have 3 file uploads that only allows a user to choose images. The probem is, whether or not a user chooses a picture with a file upload, I get the default "Images/noImage.jpg" path saved to my database. I also believe I understand why. I preset the <code>filename</code> variables to <code>""</code> or an empty string. Here is where I get confused, if I set the <code>filename</code> variables to the value of the fileupload I get a "object not set to instance of an object" error, telling me that its looking for a value that is not there. Here is what I tried:</p>
  
  <pre><code>        //ImageAlt labels current Text is set to 'Images/'
          string ImgPath1 = lblImageAlt1.Text.ToString();
          string ImgPath2 = lblImageAlt2.Text.ToString();
          string ImgPath3 = lblImageAlt3.Text.ToString();   
  
          //Get Filename from fileupload control
          string filename1 = ""; 
          string filename2 = ""; 
          string filename3 = ""; 
  
          if (fileuploadimages1.HasFile)
          {
              filename1 = Path.GetFileName(fileuploadimages1.PostedFile.FileName);
              fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));                 
          }
          else
          {
              filename1 = "noImage.jpg";
          }
  
          if (fileuploadimages2.HasFile)
          {
              filename2 = Path.GetFileName(fileuploadimages2.PostedFile.FileName);
              fileuploadimages2.SaveAs(Server.MapPath(ImgPath2 + filename2));                
          }
          else
          {
              filename2 = "noImage.jpg";
          }
  
          if (fileuploadimages3.HasFile)
          {
              filename3 = Path.GetFileName(fileuploadimages3.PostedFile.FileName);
              fileuploadimages3.SaveAs(Server.MapPath(ImgPath3 + filename3));
          }
          else
          {
              filename3 = "noImage.jpg";
          }
  
          //Save images into Images folder
          fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));
          fileuploadimages2.SaveAs(Server.MapPath(ImgPath2 + filename2));
          fileuploadimages3.SaveAs(Server.MapPath(ImgPath3 + filename3));
          //Getting SiteSqlServer2 from web.config connectionstring
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection1"].ToString());
  
          //Open the database connection
          conn.Open();
  
          //Query to insert images path and name into database
          SqlCommand cmd = new SqlCommand("Insert into my_db(CreateDate,ItemName,ItemType,ImageSrc1,ImageAlt1,ImageSrc2,ImageAlt2,ImageSrc3,ImageAlt3,ItemDescription,ItemPrice,FName,LName,ContactNum,AltContactNum,IsApproved,IsExpired,Email,IsRejected) values(@CreateDate, @ItemName, @ItemType, @ImageSrc1, @ImageAlt1, @ImageSrc2, @ImageAlt2, @ImageSrc3, @ImageAlt3, @ItemDescription, @ItemPrice, @FName, @LName, @ContactNum, @AltContactNum, @IsApproved, @IsExpired, @Email, @IsRejected)", conn);
  
          //Passing parameters to query
          cmd.Parameters.AddWithValue("@CreateDate", CreateDate);
          cmd.Parameters.AddWithValue("@ItemName", ItemName);
          cmd.Parameters.AddWithValue("@ItemType", ItemType);
  
          cmd.Parameters.AddWithValue("@ImageSrc1", filename1);
          cmd.Parameters.AddWithValue("@ImageAlt1", ImgPath1 + filename1);
  
          cmd.Parameters.AddWithValue("@ImageSrc2", filename2);
          cmd.Parameters.AddWithValue("@ImageAlt2", ImgPath2 + filename2);
  
          cmd.Parameters.AddWithValue("@ImageSrc3", filename3);
          cmd.Parameters.AddWithValue("@ImageAlt3", ImgPath3 + filename3);
  
          cmd.Parameters.AddWithValue("@ItemDescription", ItemDescription);
          cmd.Parameters.AddWithValue("@ItemPrice", ItemPrice);
          cmd.Parameters.AddWithValue("@FName", FName);
          cmd.Parameters.AddWithValue("@LName", LName);
          cmd.Parameters.AddWithValue("@ContactNum", ContactNum);
          cmd.Parameters.AddWithValue("@AltContactNum", AltNum);
          cmd.Parameters.AddWithValue("@IsApproved", isApproved);
          cmd.Parameters.AddWithValue("@IsExpired", isExpired);
          cmd.Parameters.AddWithValue("@Email", Email);
          cmd.Parameters.AddWithValue("@IsRejected", isRejected);
          cmd.ExecuteNonQuery();
  
          //Close
          conn.Close();
          Response.Redirect("form.aspx");
  
      }
  </code></pre>
  
  <p>As mentioned earlier the images will upload properly if the image path is set to <code>filename1 = Path.GetFileName(fileuploadimages1.PostedFile.FileName);....</code> but I should be able to make exceptions for users who do not wish to add all 3 or only 1 or 2 or none at all. </p>