I'm developing a ASP.NET 4.5 application which reads a log file of another application which is in server My Documents folder.
It works well when I run in debug mode but not once deployed.
Gives following error :
'C:\Users\Performance\My Folder\Log\' 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 have given Network Service and IISUSER read/write access to this file.(only this file and not folder)
This my code :
protected void lstArea_TextChanged(object sender, EventArgs e)
{
//create instance foe oledb connection class
OleDbConnection con = new OleDbConnection();
//Your datasource Location path currently i placed csv file in server location
string dsource = lstArea.SelectedValue;
//Put your datasource path in the connection string for example if you have csv files in C:\ directory change datasource= C:\
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dsource + ";Extended Properties='text;HDR=No;FMT=Delimited';";
try
{
con.ConnectionString = constr;
//create instance for command object
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
// set your file name in the below query
cmd.CommandText = "select * from [wksplog.txt]";
//Open Oledb Connection to read CSV file
con.Open();
//Create one datatable to store data from CSV file
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges);
//Bind data in the Gridview
gvMain.DataSource = dt;
gvMain.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
I'm passing the directory as C:\Users\Performance\My Folder\Log\
What is going wrong? BTW am using anonymous/ forms authentication. This machine is not in the domain.
The user the application is running as needs read access to all parent folders of the log file otherwise you will receive an access violation.
Related
I try to open a connection with a database from curent directory, and I searched on google and I found that I need to use " System.IO.Directory.GetCurrentDirectory() ". and I recive this error
Database 'D:\Work\C#\DatabaseLoginPassProj\DatabaseLoginPassProj\Database1.mdf' already exists. Choose a different database name.
Cannot attach the file 'C:\Users\Mihai\AppData\Local\Temporary Projects\BazaDeDataIndependenta\bin\Debug\Database1.mdf' as database 'Database1.mdf'.
this is my code
private void InregistrareBTN_Click(object sender, EventArgs e)
{
string connstring = #" server=.\sqlexpress;
Database = Database1.mdf;
trusted_connection = true;
AttachDBFileName = " + System.IO.Directory.GetCurrentDirectory() +
#"\Database1.mdf;";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
}
Thanks.
Ps: I don't want to specify the full path of the DB because I need to make the program working on every pc.
Firstly please don't mark it as duplicate I know it's been asked multiple times here but none of the links helped me.
I am trying to access database which is located on shared drive, also both the mdb file and the folder in which it is stored have full access to everyone. I have hosted my application on two different machines.
Below is the code to connect to access DB
OleDbDataAdapter dataAdapter = null;
DataTable dtAttendance = new DataTable();
try
{
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["AccessDBPath"].ToString();
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Mode= Share Deny None;Data Source= " + conStr))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(#"Select EmployeeId AS UserId,AttendanceDate , format(Int(Duration/60),'0') AS Duration,format(Duration Mod 60,'0') AS Remain FROM AttendanceLogs
where EmployeeId =" + userid.ToString() + " and Year(AttendanceDate)="+year+" and Month(AttendanceDate)="+month+" order by AttendanceDate desc", conn);
dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dtAttendance);
conn.Close();
}
}
catch(Exception ee)
{}
Only first time when I tested it, it worked properly and thereafter it started throwing above error.
Have no idea what I'm doing wrong here but I keep getting the following exception on the connection.Open(); line:
IErrorInfo.GetDescription failed with E_FAIL(0x80004005)
Problem is I have almost the exact same code in another batch job and it works fine. We even pull a spreadsheet from the same location. Does anyone see anything wrong with my connection or query string?
static void Main(string[] args)
{
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\prdhilfs03\l&i-sales&mkt\WORKAREA\Agencyservices\Shared\AIC\Analysts_and_Reporting\Realignments\2014\MassUpdateTesting\ZipCodeTest.xslx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
string queryString = "SELECT * FROM [Query1$]";
try
{
OleDbDataReader reader;
using (OleDbConnection connection = new OleDbConnection(connString))
{
//Set connection objects to pull from spreadsheet
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
For good measure here's a screen shot of the workbook I'm trying to connect to
Did you try to check if you can access the file?
string xlFile = #"\\prdhilfs03\l&i-sales&mkt\......\ZipCodeTest.xlsx";
Console.WriteLine(File.Exists(xlFile) ? "Excel File exists." : "Excel File does not exist.");
I am trying to load data using following code.
string path = System.IO.Path.GetFullPath(uploadExcelFile.PostedFile.FileName);
string connString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Rizwan shahid\\Desktop\\DataUpload\\Req.xls;Extended Properties=Excel 12.0;";
OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "Table");
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
oledbConn.Close();
}
It was working on 32Bit operating system but when run this code on 64Bit OS it gives the following error
The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.
I am running VS in Administrator mode and found many solution like replace Sheet1 with file name or place file in C drive but still getting the same error.
you can download latest version here of Jet
http://www.microsoft.com/en-us/download/search.aspx?q=jet
This works (I start out with a 'dummy' path and then apply the real runtime path):
OleStringBuilder =
new OleDbConnectionStringBuilder(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
OleStringBuilder.DataSource = MapPath(#"~\App_Data\MyExcelWorksheet.xls");
I'm having a problem opening a DBF file - I need to open it, read everything and process it. I tried several solutions (ODBC/OLEDB), several connection string, but nothing worked so far.
The problem is, when I execute the SQL command to get everything from the file, nothing gets returned - no rows. What's even more odd, the content of the DBF file being opened get deleted.
See the code I have:
public override bool OpenFile(string fileName, string subFileName = "")
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(fileName) + ";Extended Properties=dBASE IV;User ID=;Password=;");
try
{
if (con.State == ConnectionState.Closed) { con.Open(); }
OleDbDataAdapter da = new OleDbDataAdapter("select * from " + Path.GetFileName(fileName), con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
int i = ds.Tables[0].Rows.Count;
return true;
}
catch
{
return false;
}
}
I debugged the code and watched the file being opend in Windows Explorer. When it reached this line:
da.Fill(ds);
the size of the file dropped to only a few Bytes (from hundreds of kB).
My next thought was to make the DBF file read only. That however cause an "unexpected exception from an external driver".
So my question is - what the heck? I'm sure the file is not corrupt, it is a direct export from some DB. (No, I do not have access to that DB). I can also open that file in MS Office no problem.
I cannot share the DBF file - it contains confidential data.
Two things... just because its a .DBF file extension might night mean its a Dbase IV file. It might actually be that of Visual Foxpro. That said, I would look into downloading and installing the Visual Foxpro OleDB driver from Microsoft download. Next, the OleDbConnection is pointing to the path that has the actual tables (you already have that).
The query itself, shouldn't care about the extension, so I would change your call to get just then name via "Path.GetFileNameWithoutExtension"
It might be a combination of the two.
Connection string for VFP provider
"Provider=VFPOLEDB.1;Data Source=" + FullPathToDatabase
This is not the exact answer but it will help you to find the issue.
Try to give an inline connection string and select query to make sure problem is not with building those. Catch the exception and check the details of it.
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=;Password=;"); // give your path directly
try
{
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from tblCustomers.DBF", con); // update this query with your table name
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
int i = ds.Tables[0].Rows.Count;
return true;
}
catch(Exception e)
{
var error = e.ToString();
// check error details
return false;
}