Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am uploading csv file to sql server and at the same time binding to gridview at the same time , but i am receiving an server that ,

Could not find a part of the path 'D:\Send Free SmS\6-2-15\Files\SBM List.csv'.

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:\Send Free SmS\6-2-15\Files\SBM List.csv'.

Source Error:

Line 46: //Upload and save the file Line 47: string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); Line 48: FileUpload1.SaveAs(csvPath); Line 49: Line 50: DataTable dt = new DataTable();

Source File: d:\Send Free SmS\6-2-15\Add-Contact.aspx.cs Line: 48

protected void Upload(object sender, EventArgs e) { //Upload and save the file string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(csvPath);





C#
DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3]

    {
        new DataColumn("Name", typeof(string)),
        new DataColumn("Mobile",typeof(string)),
    new DataColumn("UserId", typeof(int))});


    string csvData = File.ReadAllText(csvPath);
    foreach (string row in csvData.Split('\n'))
    {
        if (!string.IsNullOrEmpty(row))
        {
            dt.Rows.Add();
            int i = 0;
            foreach (string cell in row.Split(','))
            {
                dt.Rows[dt.Rows.Count - 1][i] = cell;
                i++;
            }
        }
    }

    string consString = ConfigurationManager.ConnectionStrings["testsqlcon"].ConnectionString;
    using (SqlConnection con = new SqlConnection(consString))
    {
        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
        {
            //Set the database table name
            sqlBulkCopy.DestinationTableName = "dbo.addmobi";
            con.Open();
            sqlBulkCopy.WriteToServer(dt);
            con.Close();
        }
    }
}
Posted
Comments
Richard MacCutchan 8-Feb-15 14:00pm    
What part of that error message do you not understand?
BacchusBeale 8-Feb-15 14:57pm    
don't use using around your Sql objects because it is masking any exceptions that may be thrown. use try/catch and trace the stack.
Maciej Los 8-Feb-15 15:59pm    
Are you sure that file 'SBM List.csv' exists in the fallowing path 'D:\Send Free SmS\6-2-15\Files\'?

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