0

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:

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);
            }

        }
    }

Here is the InsertItemTemplate of the FormView:

<InsertItemTemplate>
                            <asp:TextBox Text='<%# Bind("venIdFk") %>' runat="server" ID="venIdFkTextBox" Visible="false" />
                            <asp:TextBox Text='<%# Bind("docPath") %>' runat="server" ID="docPathTextBox" Visible="false" />
                            <asp:TextBox Text='<%# Bind("docStat") %>' runat="server" ID="docStatTextBox" Visible="false" />
                            <asp:TextBox Text='<%# Bind("docUpldDt") %>' runat="server" ID="docUpldDtTextBox" Visible="false" />

                            <div class="row">
                                <div class="form-group col-md-3">
                                    <strong>Title</strong><br />
                                    <asp:TextBox Text='<%# Bind("docTitle") %>' runat="server" ID="docTitleTextBox" CssClass="form-control widecontrol" /><br />
                                </div>
                                <div class="form-group col-md-3">
                                    <strong>Type</strong><br />                                        
                                    <asp:DropDownList ID="ddlVenDocType" runat="server" DataSourceID="sdc_doc_type" DataTextField="dtype" DataValueField="dtypeId" SelectedValue='<%# Bind("docType") %>' AppendDataBoundItems="true" CssClass="form-control widecontrol"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList><br />
                                </div>
                                <div class="form-group col-md-3">
                                    <strong>Organization</strong><br />
                                    <asp:DropDownList ID="ddlVenDocOrg" runat="server" DataSourceID="sdc_orgs" DataTextField="org" DataValueField="orgId" SelectedValue='<%# Bind("docOrg") %>' AppendDataBoundItems="true" CssClass="form-control widecontrol"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList><br />
                                </div>
                                <div class="form-group col-md-3">
                                    <strong>Department</strong><br />                            
                                    <asp:DropDownList ID="ddlVenDocDept" runat="server" DataSourceID="sdc_depts" DataTextField="dept" DataValueField="deptId" SelectedValue='<%# Bind("docDept") %>' AppendDataBoundItems="true" CssClass="form-control widecontrol"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList><br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group col-md-3">
                                    <strong>Prior Contract Code</strong><br />
                                    <asp:TextBox Text='<%# Bind("PriorContCd") %>' runat="server" ID="PriorContCdTextBox" CssClass="form-control widecontrol" /><br />
                                </div>
                                <div class="form-group col-md-3">
                                    <strong>Legal / Compliance Contract ID</strong><br />
                                    <asp:TextBox Text='<%# Bind("LegCompContId") %>' runat="server" ID="LegCompContIdTextBox" CssClass="form-control widecontrol" /><br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group col-md-12">
                                    <strong>Description</strong><br />
                                    <asp:TextBox Text='<%# Bind("docDesc") %>' runat="server" ID="docDescTextBox" CssClass="form-control widecontrol" Rows="5" TextMode="MultiLine" /><br />
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group col-md-12">
                                    <asp:FileUpload ID="fu_doc_upld" runat="server" />
                                    <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">*</asp:RequiredFieldValidator>
                                </div>
                            </div>    

                            <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" CssClass="cmdlinkpdg" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" CssClass="cmdlinkpdg" />

                        </InsertItemTemplate>

I'm not getting any errors, it just doesn't save the file.

4
  • I added required field validator to the FileUpload control, and now I see the issue. I browse to and choose a file. The filename appears in the FileUpload control, but when I press the "Insert" button the filename is removed from the FileUpload control and the requried field validator is triggered. Commented Aug 3, 2016 at 16:04
  • could you write the pageload method? Commented Aug 3, 2016 at 16:16
  • I don't have anything in Page_Load. Commented Aug 3, 2016 at 16:36
  • I removed the UpdatePanel and now the control is back to the original issue. It saves the record data into the database correctly, including the correct path of the file, but it doesn't save the file to the Data folder. The RequiredFieldValidator isn't tripped upon inserting. Commented Aug 3, 2016 at 16:54

1 Answer 1

0

OK, it helps to stop and think about what actually needs to happen. I moved the filename / path and upload to OnItemInserting and it all works now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.