Server Error in application while importing .csv file to Database using asp .net c# RRS feed

  • Question

  • User2029691935 posted


    I have designed one web application with web form for importing .csv file into sql database table. I have taken one File up-loader and one button to importing data into database. I am able to import .csv file on IIS server; but not on azure server, after deploying on Azure , it is not working. 

    Error as follows:-

    Server Error in '/' Application.

    Could not find a part of the path 'D:\home\site\wwwroot\App_Data\UploadedFiles\'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\wwwroot\App_Data\UploadedFiles\'.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 

    [DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\wwwroot\App_Data\UploadedFiles\'.]
       System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +353
       System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1326
       System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +60
       System.IO.FileStream..ctor(String path, FileMode mode) +55
       System.Web.HttpPostedFile.SaveAs(String filename) +94
       CRM_CSharp_1.Customer_Details.Button2_Click(Object sender, EventArgs e) +159
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

    Please do needful.

    Thanks & Regards


    Tuesday, April 12, 2016 11:06 AM

All replies

  • User-821857111 posted

    Looks like you have hard-coded a file system path somewhere in your application that doesn't exist in your Azure space. You should use Server.MapPath to resolve virtual paths to absolute ones:

    var path = Server.MapPath("~/App_Data/UploadedFiles");

    Tuesday, April 12, 2016 12:17 PM
  • User-646145796 posted


    As Mike said, please check your csv file to see whether the path is correct. KUDU is a good tool for us to do this. Refer to https://blogs.msdn.microsoft.com/benjaminperkins/2014/03/24/using-kudu-with-windows-azure-web-sites/ for more details about how to use KUDU to check files.

    Best Regards,


    Wednesday, April 13, 2016 1:15 AM
  • User2029691935 posted

    Hello Sir,

    I have used Server.MapPath to resolve virtual path to absolute path; but still i am getting same Server Error in Application. For reference added my application code for importing csv file to SQL database.

       //Import CSV Data To Sql Database
            protected void Button2_Click(object sender, EventArgs e)
                //Upload and save the file
                //var path = Server.MapPath("~/App_Data/UploadedFiles");
                string csvPath = Server.MapPath("~/App_Data/UploadedFiles") + Path.GetFileName(FileUpload1.PostedFile.FileName);
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("CustNameAndAddress", typeof(string)),
                new DataColumn("CustomerDept",typeof(string)),
                new DataColumn("CustomerDesignation",typeof(string)),
                new DataColumn("CustomerContactNo",typeof(string)),
                new DataColumn("CustomerEmail",typeof(string)) });
                string csvData = File.ReadAllText(csvPath);
                foreach (string row in csvData.Split('\n'))
                    if (!string.IsNullOrEmpty(row))
                       int i = 0;
                       foreach (string cell in row.Split(','))
                           dt.Rows[dt.Rows.Count - 1][i] = cell;
                 //string consString = "Data Source=RUSHALI-PC\\SQLEXPRESS; Initial Catalog = MyTestDB; Integrated Security=True";
                 string consString = "Server=tcp:mytrackingdb.database.windows.net,1433;Database=MyTrackingDB;User ID=-------;Password=-------;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
                 using (SqlConnection con = new SqlConnection(consString))
                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                      //Set the database table name
                      sqlBulkCopy.DestinationTableName = "dbo.Customer_Details";

    Please do needful.

    Thanks & Regards


    Wednesday, April 13, 2016 5:06 AM
  • User-821857111 posted

    You should use Path.Combine to generate file paths instead of concatenating strings:

    string csvPath = Path.Combine(Server.MapPath("~/App_Data/UploadedFiles"), Path.GetFileName(FileUpload1.PostedFile.FileName));

    If you rely on concatenated strings, it's easy to forget the path separator, which is what happened in your code.

    Wednesday, April 13, 2016 7:46 AM
  • User2029691935 posted

    Hello Sir,

     I have replaced with this line of code, but after deploying the application on Azure, still i am getting same Server Error Application. Please do help me.

    Thanks & Regards


    Wednesday, April 13, 2016 9:35 AM