We help IT Professionals succeed at work.

ASP FileUpload object is always empty

Oops, I accidentally made this a 500 point question and can't seem to change it.  Anyhow, it shouldn't be tough.

I'm simply trying to get the filename select by the user for an upload.  I've spent two hours and can't figure out why it returns an empty string in my code-behind.

A couple of notes:
All of the other controls show their values without issues.  
Intellisense sees "txtFileUpload.FileName"
It is within a form tag.  
It is not in any UpdatePanel.  It IS withing a contentplaceholder in case that has something to do with it.
When I break my code, I switch back to the web page and see the FileUpload value still there.

Lost...  Please help.  Code below
Here's the ASPX code:

                    <td><b>Select File:</b></td>
                      <asp:FileUpload ID="txtFileName" runat="server" Width="350" />                                            
                      <ASP:LinkButton ID="LinkButton1" runat="server" text="Upload Song" />
                      <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtFileName" ErrorMessage="Select a File"></asp:RequiredFieldValidator>

                           OnClientProgressStopped   = "function(){alert('Track uploaded to server!  Thanks.');}"
                           OnClientProgressStarted   = "Clear"
                           OnClientFileRejected      = "Rejected"
                           ShowUploadedFiles         = "true"
                           runat                     = "server"
                           ID                        = "uploadProgress"
                           InnerFiles                = "true"
                           StyleFile                 = "fup_styles/myStyle.css">
                                <fup:Format Ext="mp3"/>
                                <fup:Format Ext="wav"/>
                                <fup:Format Ext="txt"/>
                      <fup:StatusPanel ID="StatusPanel1" runat="server">
                        <asp:Label runat="server" id="uploadedFiles" Text="" />
- C# Code:

            string strSQL = "";
            string cnnString = ConfigurationManager.ConnectionStrings["SmashHausConnectionString"].ConnectionString;
            SqlConnection cn = new SqlConnection(cnnString);

            strSQL = "INSERT INTO metadata (CreationDate, username, Filename, Time, Description, TrackTitle, Category, " + 
                     " SubCategory,BPM,Keywords, " +
                     " FeaturedInstrument, CDTitle, IsVocal) " +
            " VALUES (getdate()," +
                     "'jtsirota','" +                     
                     txtFileName.FileName.ToString().Replace("'", "''") + "','" +
                     txtLength.Text.Replace("'", "''") + "','" +
                     txtDescription.Text.Replace("'", "''") + "','" +
                     txtTrackTitle.Text.Replace("'","''") + "','" +
                     ddCategory.SelectedValue.Replace("'", "''") + "','" +
                     ddSubCategory.SelectedValue.Replace("'", "''") + "','" +
                     txtBPM.Text.Replace("'", "''") + "','" +
                     txtKeywords.Text.Replace("'", "''") + "','" +
                     txtFeaturedInst.Text.Replace("'", "''") + "','" +
                     txtCDTitle.Text.Replace("'", "''") + "','" + 
                     ddIsVocal.SelectedValue + "')";


Open in new window

Watch Question

txtFileName.FileName.ToString().Replace("'", "''")

what is the txtFileName ?
is it a textbox ?
if it is, there is no such a property named "FileName" , use "Text" instead.
like : txtFileName.Text.ToString().Replace("'", "''")

if it's an openFileDialog Control
then if you want to get the just the file name and it's extension (without the path) you can use the property
like : txtFileName.SafeFileName.ToString().Replace("'", "''")

best regards
Are you sure UpdatePanel is really not used?

Please try to use enctype form attribute:
<form id="form1" runat="server" enctype="multipart/form-data">


I put the enctype in my form tag, and no luck.

And, no I'm not using an UploadPanel, I check and double checked.  I even removed my masterpage code and ran the aspx page and it failed.

Here's my entire aspx page - pretty simple, so I'm really confused.:
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Can you check to make sure that
is true before you check

Just in case something else is going on...


CORRECTION!  It IS sort-of using an updatepanel... You were right Dmitry.  I'm using a 3rd party File upload progress control that uses what it calls a status panel, like so:
                      <fup:StatusPanel ID="StatusPanel1" runat="server">
                        <asp:Label runat="server" id="uploadedFiles" Text="" />

When I removed that it worked.  But I do need that, so what's the work around?
try this code in addition to enctype:

protected override void OnInit(EventArgs e)
    ScriptManager manager = ScriptManager.GetCurrent(Page);
    if (manager != null)

If you have no scriptmanager on your page you need to add it