Click here to Skip to main content
14,920,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using text box,browse button and upload button in c# window application...
i get the file path in text box...but i click the upload button its shows some error.........

Button browse code
C#
private void btnbrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                               
            }
        }

Button upload code
C#
private void btnupload_Click(object sender, EventArgs e)
     {

         HttpFileCollection uploads = HttpContext.Current.Request.Files;
         for (int i = 0; i < uploads.Count; i++)
         {
             //HttpPostedFile upload = uploads[i];


             if (upload.ContentLength == 0)
                 continue;


             string c = lbl1filename.Text.ToString();
             //string filename = System.IO.Path.GetFileNameWithoutExtension(upload.FileName);
             //string extension = System.IO.Path.GetExtension(upload.FileName);

             string nfilepath;
             nfilepath = "D:\\saravana\\Spatiotemporal Approach\\SourceMain\\source_uploadfiles\\" + c;
             try
             {
                 upload.SaveAs("D:\\saravana\\Spatiotemporal Approach\\SourceMain\\source_uploadfiles\\" + c);
                 //Span1.InnerHtml = "Upload(s) Successful.";


             }

             catch (Exception Exp)
             {
                 //Span1.InnerHtml = "Upload(s) FAILED.";
                 Exp.ToString();
             }
             SqlCommand cmd = new SqlCommand();
             cmd.Connection = con;
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "insert into filedetails(filename,filetype,filepath) values('" + lbl1filename.Text + "','" + nfilepath + "')";
             if (con.State == ConnectionState.Closed)
             {
                 con.Open();
                 cmd.ExecuteNonQuery();
                 MessageBox.Show("Uploaded suxcessfully");

             }
             if (con.State == ConnectionState.Open)

                 con.Close();

         }
     }
Posted

C#
if (FileUploadControl.HasFile)
               {
                   try
                   {
                       filename = Path.GetFileName(FileUploadControl.FileName);
                       FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                       objInwardLetter.strPath = "~/" + filename;

}
catch
{}
}
   
Try debugging through the code.
Note where you get the error.

Check if the file exists at the hardcoded file path.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900