title: How to save a file using the asp.net fileupload control?
body: <p>I have an asp.net webforms c# application that has a FileUpload control inside the InsertItemTemplate of a FormView.  When inserting the new record the correct path + filename is saved to the appropriate field in the database, but the file isn't saved to the Data folder.  Here is the code behind:</p>
  
  <pre><code>protected void fv_ven_doc_det_DataBound(object sender, EventArgs e)
      {
          if (fv_ven_doc_det.CurrentMode == FormViewMode.Insert)
          {
              //Set the fk                
              TextBox venIdFkTxt = (TextBox)fv_ven_doc_det.FindControl("venIdFkTextBox");
              if (venIdFkTxt != null)
              {
                  venIdFkTxt.Text = hdn_ven_id.Value;
              }
  
              //Get the upload date - current date and time
              hdn_doc_upload_dt_txt.Value = DateTime.Now.ToString();
              string docuplddt = fv_ven_doc_det.FindControl("docUpldDtTextBox").ToString();
              docuplddt = hdn_doc_upload_dt_txt.Value;
  
              TextBox docuplddttxt = (TextBox)fv_ven_doc_det.FindControl("docUpldDtTextBox");
              if (docuplddttxt != null)
              {
                  docuplddttxt.Text = hdn_doc_upload_dt_txt.Value;
              }
  
              //Set the current status
              TextBox crntDocStatTxt = (TextBox)fv_ven_doc_det.FindControl("docStatTextBox");
              if (crntDocStatTxt != null)
              {
                  crntDocStatTxt.Text = "4";
              }
  
              //Set the filename and save the file
              int i = 0;
  
              FileUpload fu = (FileUpload)(fv_ven_doc_det.FindControl("fu_doc_upld"));
              string filename = fu.FileName;
  
              if (fu.HasFile == true)
              {
                  while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
                  {
                      i++;
                      filename = fu.FileName + " (" + i.ToString() + ")";                        
                  }
                  fu.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
              }
  
              TextBox filenameTxt = (TextBox)fv_ven_doc_det.FindControl("docPathTextBox");
              {
                  filenameTxt.Text = (Server.MapPath("~/Data/") + filename);
              }
  
          }
      }
  </code></pre>
  
  <p>Here is the InsertItemTemplate of the FormView:</p>
  
  <pre><code>&lt;InsertItemTemplate&gt;
                              &lt;asp:TextBox Text='&lt;%# Bind("venIdFk") %&gt;' runat="server" ID="venIdFkTextBox" Visible="false" /&gt;
                              &lt;asp:TextBox Text='&lt;%# Bind("docPath") %&gt;' runat="server" ID="docPathTextBox" Visible="false" /&gt;
                              &lt;asp:TextBox Text='&lt;%# Bind("docStat") %&gt;' runat="server" ID="docStatTextBox" Visible="false" /&gt;
                              &lt;asp:TextBox Text='&lt;%# Bind("docUpldDt") %&gt;' runat="server" ID="docUpldDtTextBox" Visible="false" /&gt;
  
                              &lt;div class="row"&gt;
                                  &lt;div class="form-group col-md-3"&gt;
                                      &lt;strong&gt;Title&lt;/strong&gt;&lt;br /&gt;
                                      &lt;asp:TextBox Text='&lt;%# Bind("docTitle") %&gt;' runat="server" ID="docTitleTextBox" CssClass="form-control widecontrol" /&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                                  &lt;div class="form-group col-md-3"&gt;
                                      &lt;strong&gt;Type&lt;/strong&gt;&lt;br /&gt;                                        
                                      &lt;asp:DropDownList ID="ddlVenDocType" runat="server" DataSourceID="sdc_doc_type" DataTextField="dtype" DataValueField="dtypeId" SelectedValue='&lt;%# Bind("docType") %&gt;' AppendDataBoundItems="true" CssClass="form-control widecontrol"&gt;&lt;asp:ListItem Value=""&gt;--Please Select--&lt;/asp:ListItem&gt;&lt;/asp:DropDownList&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                                  &lt;div class="form-group col-md-3"&gt;
                                      &lt;strong&gt;Organization&lt;/strong&gt;&lt;br /&gt;
                                      &lt;asp:DropDownList ID="ddlVenDocOrg" runat="server" DataSourceID="sdc_orgs" DataTextField="org" DataValueField="orgId" SelectedValue='&lt;%# Bind("docOrg") %&gt;' AppendDataBoundItems="true" CssClass="form-control widecontrol"&gt;&lt;asp:ListItem Value=""&gt;--Please Select--&lt;/asp:ListItem&gt;&lt;/asp:DropDownList&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                                  &lt;div class="form-group col-md-3"&gt;
                                      &lt;strong&gt;Department&lt;/strong&gt;&lt;br /&gt;                            
                                      &lt;asp:DropDownList ID="ddlVenDocDept" runat="server" DataSourceID="sdc_depts" DataTextField="dept" DataValueField="deptId" SelectedValue='&lt;%# Bind("docDept") %&gt;' AppendDataBoundItems="true" CssClass="form-control widecontrol"&gt;&lt;asp:ListItem Value=""&gt;--Please Select--&lt;/asp:ListItem&gt;&lt;/asp:DropDownList&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="form-group col-md-3"&gt;
                                      &lt;strong&gt;Prior Contract Code&lt;/strong&gt;&lt;br /&gt;
                                      &lt;asp:TextBox Text='&lt;%# Bind("PriorContCd") %&gt;' runat="server" ID="PriorContCdTextBox" CssClass="form-control widecontrol" /&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                                  &lt;div class="form-group col-md-3"&gt;
                                      &lt;strong&gt;Legal / Compliance Contract ID&lt;/strong&gt;&lt;br /&gt;
                                      &lt;asp:TextBox Text='&lt;%# Bind("LegCompContId") %&gt;' runat="server" ID="LegCompContIdTextBox" CssClass="form-control widecontrol" /&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="form-group col-md-12"&gt;
                                      &lt;strong&gt;Description&lt;/strong&gt;&lt;br /&gt;
                                      &lt;asp:TextBox Text='&lt;%# Bind("docDesc") %&gt;' runat="server" ID="docDescTextBox" CssClass="form-control widecontrol" Rows="5" TextMode="MultiLine" /&gt;&lt;br /&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="form-group col-md-12"&gt;
                                      &lt;asp:FileUpload ID="fu_doc_upld" runat="server" /&gt;
                                      &lt;asp:RequiredFieldValidator ID="rfv_fu_doc_upld" runat="server" ErrorMessage="A file must be chosen to upload." ControlToValidate="fu_doc_upld" ForeColor="#ff0000" Font-Bold="true"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;    
  
                              &lt;asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" CssClass="cmdlinkpdg" /&gt;&amp;nbsp;&lt;asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" CssClass="cmdlinkpdg" /&gt;
  
                          &lt;/InsertItemTemplate&gt;
  </code></pre>
  
  <p>I'm not getting any errors, it just doesn't save the file.</p>