Why would my fileupload control stop working?

Hello Experts.  

I have a fileupload control in my asp.net c# web forms application that just stopped working.  It worked just fine the last time I used it - about 2 weeks ago.  Now I can select a file and then click the save button and nothing happens.  I don't get any errors, but no file is uploaded to the Data folder.  The file upload form is located in the root along with Default.aspx.  Any help would be greatly appreciated.

I am using VS2013.

Here's the code I'm using:

protected void btn_frm_new_doc_save_close_Click(object sender, EventArgs e)
        {
            int i = 0;
            string filename = fu_doc_upld.FileName;
            if (fu_doc_upld.HasFile)
            {
                while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
                {
                    i++;
                    filename = fu_doc_upld.FileName + " (" + i.ToString() + ")";
                }
            }

            hdn_filename_txt.Value = (Server.MapPath("~/Data/") + filename);
            hdn_doc_uplod_dt_txt.Value = DateTime.Now.ToString();

            SqlConnection idrf_cnxn = new SqlConnection("Data Source=xxx\\xxx;Initial Catalog=idrf;Integrated Security=True");
            {
                SqlCommand new_doc_cmd = new SqlCommand("Insert Into tbl_doc(doc_title, doc_type_list, doc_org_list, doc_dept_list, doc_desc, prior_contract_cd, legal_comp_contract_id, doc_upld_dt, doc_path, vendor_id_fk) Values(LTRIM(RTRIM(@doc_title)), LTRIM(RTRIM(@doc_type_list)), LTRIM(RTRIM(@doc_org_list)), LTRIM(RTRIM(@doc_dept_list)), LTRIM(RTRIM(@doc_desc)), LTRIM(RTRIM(@prior_contract_cd)), LTRIM(RTRIM(@legal_comp_contract_id)), LTRIM(RTRIM(@doc_upld_dt)), LTRIM(RTRIM(@doc_path)), LTRIM(RTRIM(@vendor_id_fk)))", idrf_cnxn);

                new_doc_cmd.Parameters.AddWithValue("@doc_title", doc_title_txt.Text);
                new_doc_cmd.Parameters.AddWithValue("@doc_type_list", doc_type_ddl.Text);
                new_doc_cmd.Parameters.AddWithValue("@doc_org_list", doc_org_ddl.Text);
                new_doc_cmd.Parameters.AddWithValue("@doc_dept_list", doc_dept_ddl.Text);
                new_doc_cmd.Parameters.AddWithValue("@doc_desc", doc_desc_txt.Text);
                new_doc_cmd.Parameters.AddWithValue("@prior_contract_cd", prior_contract_cd_txt.Text);
                new_doc_cmd.Parameters.AddWithValue("@legal_comp_contract_id", lgl_comp_cont_id_txt.Text);
                new_doc_cmd.Parameters.AddWithValue("@doc_upld_dt", hdn_doc_uplod_dt_txt.Value);
                new_doc_cmd.Parameters.AddWithValue("@doc_path", hdn_filename_txt.Value);
                new_doc_cmd.Parameters.AddWithValue("@vendor_id_fk", hdn_vendor_id_txt.Value);

                idrf_cnxn.Open();
                new_doc_cmd.ExecuteNonQuery();
                idrf_cnxn.Close();

                if (IsPostBack)
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }

Open in new window


Thanks,
J
ferguson_jeraldAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
Do you get any errors? maybe try trapping
jshesekCommented:
You know IIS has a file size limit on it.  Did you upload a large file?  
Here's a link to check what file size it is:  http://www.web-site-scripts.com/knowledge-base/article/AA-00696/0/Increasing-maximum-allowed-size-for-uploads-on-IIS7.html
ferguson_jeraldAuthor Commented:
My apologies, for the delayed response.  

I do not get any errors.  I attempted trapping, but that didn't work.  I tested it with a smaller file as well but it still didn't work.  

Next I attempted to add
fu_doc_upld.SaveAs(Server.MapPath("~/Data/") + filename);

Open in new window

to the While, but it still didn't work, and I still didn't get any errors.  The information goes into the database just fine, but the files just aren't uploading to the "Data" folder.

Any other ideas would be greatly appreciated.
Randy DownsOWNERCommented:
It sounds like the code is just not running. Can you monitor it with Console in browser?

Here's some more code you could try.

(WebForm1.aspx) and its related code-behind file (WebForm1.aspx.cs) to upload files to a directory that is named Data.

Web form.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CSharpUpload.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" enctype="multipart/form-data" runat="server">
<INPUT type=file id=File1 name=File1 runat="server" >
<br>
<input type="submit" id="Submit1" value="Upload" runat="server" NAME="Submit1">
</form>

	
  </body>
</HTML>

Open in new window


aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CSharpUpload
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputFile File1;
		protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			// 
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			// 
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Submit1_ServerClick(object sender, System.EventArgs e)
		{
			if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
			{
				string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
				string SaveLocation = Server.MapPath("Data") + "\\" +  fn;
				try
				{
					File1.PostedFile.SaveAs(SaveLocation);
					Response.Write("The file has been uploaded.");
				}
				catch ( Exception ex )
				{
					Response.Write("Error: " + ex.Message);
					//Note: Exception.Message returns a detailed message that describes the current exception. 
					//For security reasons, we do not recommend that you return Exception.Message to end users in 
					//production environments. It would be better to return a generic error message. 
				}
			}
			else
			{
				Response.Write("Please select a file to upload.");
			}
		}
	}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ferguson_jeraldAuthor Commented:
OK, so the issue was that I put the SaveAs in the While loop.  I moved it out of there and it works just fine now.  

if (fu_doc_upld.HasFile)
            {
                while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
                {
                    i++;
                    filename = fu_doc_upld.FileName + " (" + i.ToString() + ")";                    
                }
                fu_doc_upld.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
            }

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.