0

I am using the below two codes to read the csv file but it returns the same error.

'E:\business\business\Document\4112013\20580.csv' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

I'm not able to fix this issue. Plese help me to solve this

SaveLocation = @"E:\business\business\Document\4112013\20580.csv";
string connStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=@" + SaveLocation + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\"";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
// give full path to the file here
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + SaveLocation, conn);
DataSet ds = new DataSet("QueryCSV");
adapter.Fill(ds);
DataTable dt = ds.Tables[0];


System.Data.OleDb.OleDbDataAdapter MyCommand = default(System.Data.OleDb.OleDbDataAdapter);
System.Data.OleDb.OleDbConnection MyConnection = default(System.Data.OleDb.OleDbConnection);
//MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; " + "data source=" + SaveLocation + "; " + "Extended Properties=Excel 12.0;");
//MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; " + "data source=" + SaveLocation + "; " + "Extended Properties='csv;HDR=Yes;FMT=Delimited(,)';");

MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + SaveLocation + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\"");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet5$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
user2176150
  • 303
  • 3
  • 12
  • 21
  • A quick google of "CSV to Dataset C#" : About 10,400,000 results. Top 5 all good pages/answers. – Ryan McDonough Apr 11 '13 at 07:11
  • And is it a valid path? I notice it has "business/business" in it..is that correct? Try copy and pasting the path from above into Explorer... – Lee Gunn Apr 11 '13 at 07:11
  • it is not duplicate his title is inappropriate – Satpal Apr 11 '13 at 07:15
  • Try to put path as only @"E:\sundss_\business\business\Document\4112013. in my application i does not give filename at the end but only the upto the directory where it is located – Rajeev Kumar Apr 11 '13 at 07:18

2 Answers2

0

This smells more like security issue then wrong path issue. You can not from ASP.NET web applicaiton access any parts of your hard drive. This is for security reasons, so potential atacker would not be able to do the same, in case of your site's broken security.

Put you csv file into the your web applicaiton subdirectory, and access it with combination of one of the following properties:

HttpRuntime.AppDomainAppPath

or with

HttpContext.Current.Request.PhysicalApplicationPath

Having relative and not fixed path brings you other benefits, that after upload your aplcation to a server, or to another server, you have not change anything in reagard of this. Evrything keeps working as before.

For moe information look here

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

You put this as file path, It will work.

string strDataSource = "E:\business\business\Document\4112013\20580.csv";
strDBFile = strDataSource.Substring(0, strDataSource.LastIndexOf('\\'));
mCon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strDBFile + ";Extended Properties=\"Text;Excel 12.0;HDR=No;IMEX=1;FMT=Delimited\"";
user2176150
  • 303
  • 3
  • 12
  • 21
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83