Reputation: 29
I followed Jim Scott's instruction to read CSV into a DataTable
with the following code:
private OleDbConnection CNN = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Directory.GetCurrentDirectory()+";Extended Properties=\"Text;HDR=Yes\"");
private OleDbCommand CMD;
private OleDbDataAdapter ADT;
private DataTable DT=new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
CNN.Open();
CMD = new OleDbCommand(@"select * from [Report.csv]", CNN);
ADT = new OleDbDataAdapter(CMD);
ADT.Fill(DT);
}
I had put the Report.csv
under the root directory, and tried following things to no avail!
Changed Data Source
to localhost
, (localhost)
, ~
, ~\\
.
Changed Report.csv
to Report
.
Finally change Data Source
to Directory.GetCurrentDirectory()
to get it to connect correctly.
Problem: It can NOT find Report.csv
!
I wish this to be a webpage, so what I need is a way for the
OleDbConnection
connect tolocalhost
and pointed to the root directory!It will be VERY nice if somebody could teach me how to do that!
Somebody please be so kind and tell me where did I do wrong and how to set it up correctly!
Much appreciated!!!
Upvotes: 0
Views: 105
Reputation: 6522
Most likely, its a path issue. Can you try doing the following:
protected void Page_Load(object sender, EventArgs e)
{
CNN.Open();
string fileName = "C:\Users\username\Desktop\Report.csv";
string sqlQuery = @"select * from [" + fileName + "]";
CMD = new OleDbCommand(sqlQuery, CNN);
ADT = new OleDbDataAdapter(CMD);
ADT.Fill(DT);
}
Update:
Instead of Directory.GetCurrentDirectory()
, you can use HttpContext.Current.Server.MapPath("~")
which will give the path of the current root.
If your website is at:
C:\Web\shop
and you are accessing webpage: http://localhost:8080/Application/users/userdetails.aspx
1. Server.MapPath(".") returns C:\Web\shop\users
2. Server.MapPath("~") returns C:\Web\shop\
Upvotes: 3