Hello,
A new day and fresh mind. Yes, your are right I am doing some unnecessary
checks, but I was only doing that to find out what was the problem. It
didn't dawn on me until this morning that the following code:
string strSourceDirectory="C:\\Documents and Settings\\loescsc\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
Found the directory path just fine. I tested this by giving
strSourceDirectory a bogus directory and sure enough it failed immediately
just like you said it would. The Exception error it's giving me is
misleading then. Here is the code in my ProcessFile() where I get the
exception error:
try
{
Database db = DatabaseFactory.CreateDatabase("Microsoft.Jet.OLEDB.4.0",
strDBSourceDirectory, DBProviderType.OLEDB);
//string sqlCommand = "Select * From [ExtractData]";
string sqlCommand = "Select * From " + strDBSourceDirectory;
DBCommandWrapper dbCommandWrapper =
db.GetSqlStringCommandWrapper(sqlCommand);
StringBuilder readerData = new StringBuilder();
using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
DataTable newDataTable = new DataTable();
newDataTable= CreateDataTable("AP");
// Iterate through DataReader
while (dataReader.Read())
{
DataRow row;
row = newDataTable.NewRow();
// Get the value of the 'Name' column in the DataReader
rowcount++;
row["Invoice_No"] = dataReader["Invoice_No"].ToString();
row["Invoice_Date"] = dataReader["Invoice_Date"].ToString();
row["Equipment_ID"] = dataReader["Equipment_ID"].ToString();
row["Invoice_Line_Item_Amount"] =
dataReader["Invoice_Line_Item_Amount"].ToString();
row["Authorization_No"] = dataReader["Authorization_No"].ToString();
newDataTable.Rows.Add(row);
}//End while (dataReader.Read())
if (dataReader != null) dataReader.Close();
}//End using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
}//End try
catch (Exception ex)
{
// Print error message
Console.WriteLine("Error: {0}",ex.Message.ToString());
}//End catch
This code worked fine in with .xls files, but it should work with .txt and
.csv files as well. To read .xls files I had this line of code
connectionStringData.Parameters.Add(new ParameterData("Extended
Properties",
"'Excel 8.0;HDR=Yes;IMEX=1'"));
And I changed it to this for the .txt and .csv files:
connectionStringData.Parameters.Add(new ParameterData("Extended
Properties",
"'text;HDR=Yes;FMT=Delimited(,)'"));
I'm using Microsoft Enterprise Library to do my database connections. I
still don't know why it gives this Exception:
@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.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 can take C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv and paste it in Windows Explorer,
and
it will open up the file just fine.
Willy Denoyette said:
| Here is the code I am using:
|
| string strSourceDirectory="C:\\Documents and Settings\\admin\\My
| Documents\\Excel\\Excel\\";
| string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
| string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
|
| foreach(string strFileName in strarrFileNames)
| {
| if(Directory.Exists(strSourceDirectory))
| {
| // This path is a directory
| if(File.Exists(strFileName))
| {
| // This path is a file
| ProcessFile(strFileName);
| }
| else
| {
| // No File Found
| }
| }
| else
| {
| // No such directory
| }
|
| }// End foreach(string strFileName in strarrFileNames)
|
| I've tried declare the string both ways, with @ and with out and the
value
| is always the same @"C:\Documents and Settings\admin\My
| Documents\Excel\Excel\"
|
| As for the Permissions, I have Admin rights to my box, so I have access
to
| this directory. Just for simplicity if I declare string
| strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
| @"C:\temp\somedirectory\"
|
|
Forget about the @ and tell us which statement throws an exception and
what
exception.
Note that you are doing a lot of superfluous checks in your code, if you
call GetFiles, you will get back a list of files in an array
(strarrFileNames), right? Why do you check for the existing of the
directory
and the file, if they didn't exist they wouldn't be in the array. That
means
you can trim your foreach and keep only:
foreach(string strFileName in strarrFileNames)
{
// This path is a file
ProcessFile(strFileName);
}
Willy.