I'm currently facing a problem whereby I don't know how to fix it.
I upload my precompiled project into IIS. Here is my purpose of this page:
User upload a excel file into a folder in the server. ex #"~/PlanQuantityFile/". It did upload successfully.
Problems faced:
It stops when my scripts trying to open the excel file (for extract data purpose) without showing any errors. At line 881 as shown in the image.
Here is few area I had seek for but it still couldn't solve my problem.
Possible Solution:
connection open but never close, so run out of connection. (but I did close it and the scripts stop running before the close statement)
32 bit program calling 64 bit office. (I had limited knowledge on hardware field, don't know what should I do to troubleshoot here)
permission problem. Need to set the permission of ASP.NET account. (I still finding object names for ASP.NET account)
Thanks for anyone who trying to help. Your advice is invaluable.
OleDbConnection oledbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + SaveLocation + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
OleDbConnection connExcel = oledbConn;
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
//Get the name of Sheet
try
{
connExcel.Open();// It stops here without showing errors.
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex.Message + "');", true);
}
The following code returns a datatable for selected filelocation source. Remember to rename the sheetname to "Sheet1".
Use Namespace: using System.Data.OleDb;
Function::::
public DataTable GetExcelinDatatable(string filelocation)
{
DataTable dt = new DataTable();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
con.Open();
OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("select * from [SHEET1$]", con);
da.Fill(dt);
con.Close();
return dt;
}
you can use mentioned function to read the Excel file you have to just pass the path of excel file
private List<DataTable> readExcel(string strXLS)
{
//DataTable dtExcel = getExcelSheetTable();
List<DataTable> SheetsData = new List<DataTable>();
DataTable dtExcel = new DataTable();
DataTable SocialMediaExcel = new DataTable();
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strXLS + ";Extended Properties=Excel 12.0;";
try
{
OleDbDataAdapter adpt = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connStr);
adpt.Fill(dtExcel);
SheetsData.Add(dtExcel);
}
catch (Exception ex)
{
throw ex;
}
return SheetsData;
}
Related
I need to fetch a row value from an excel sheet in data grid view in winform.
I’m able to display the entire excel sheet in the datagridview. But, I need to display particular rows in the grid based on a current date condition.
public DataTable ReadExcel2(string fileName, string fileExt)
{
string connectionstring ;
DataTable dtexcel2 = new DataTable();
connectionstring = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=YES';";
OleDbConnection connection = new OleDbConnection(connectionstring);
OleDbCommand oconn = new OleDbCommand("Select * From [POSFailures$] WHERE Date=#date");
oconn.Connection = connection;
try
{
oconn.Parameters.AddWithValue("#date", DateTime.Now.ToString("MM/dd/yyyy"));
connection.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
sda.Fill(dtexcel2);
connection.Close();
}
catch (Exception)
{
}
return dtexcel2;
}
Thanking you in advance
What seems to be happening here is that the Date parameter is not being honored, as you are getting back all of the rows. So, I used Google to figure out how to properly add parameters when using OleDbConnection. I found this:
The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement
Source: https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbcommand.parameters?redirectedfrom=MSDN&view=netframework-4.8#System_Data_OleDb_OleDbCommand_Parameters
Using the example on that page, try changing your code to this:
string connectionstring;
DataTable dtexcel2 = new DataTable();
connectionstring = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=YES';";
OleDbConnection connection = new OleDbConnection(connectionstring);
OleDbCommand command = new OleDbCommand("Select * From [POSFailures$] WHERE Date = ?");
command.Parameters.Add(new OleDbParameter(DateTime.Now.ToString("MM/dd/yyyy"), OleDbType.Date));
command.Connection = connection;
try
{
connection.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(command);
sda.Fill(dtexcel2);
connection.Close();
}
catch (Exception)
{
}
Please note that i've not tested this, so I can't promise it will work. But, the main point is... the answer is out there! You just need to go looking for it.
for a small project I need to create a DataTable filled with data from an Excelsheet. Im getting this data with using an Oledb connection. Sometimes other people are using this file on the server. So I'm getting the "The process cannot access the file because it is being used by another process"
private DataTable excelSelectStatement() {
try {
string par = this._colParameters.getParameterByName("SheetName", false, null).Value;
excelDataSet = new DataSet();
string ace12 = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + _selectionStatement + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';";
using (OleDbConnection connection = new OleDbConnection(ace12)) {
OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + par + "$]", connection);
da.TableMappings.Add("Table", "TestTable");
da.Fill(excelDataSet);
}
}
catch (Exception e) { MessageBox.Show(e.Message); }
return excelDataSet.Tables[0];
}
Is there any way with completing this with Oledb?
I have an excel file and an oledb connection to it. When reading data while file is opened in windows, it throws the following error (at Adapter.Fill method).
However, the code runs fine when file is not opened manually.
private System.Data.DataSet GetExcelData()
{
// Create new DataSet to hold information from the worksheet.
System.Data.DataSet objDataset1 = new System.Data.DataSet();
DataTable dt = new DataTable();
try
{
string path = ConfigurationManager.AppSettings["ExcelFilePath"];
//string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
OleDbConnection objConn = new OleDbConnection(connectionString);
objConn.Open();
//String strConString = "SELECT * FROM [Data Version5.2$A2:ZZ] where [Status] = 'aa'";//Status
String strConString = "SELECT * FROM [Data Version5.2$A2:ZZ] where [Status] IS NULL OR [Status]='SubReport'";//Status SubReport
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Fill the DataSet with the information from the work sheet.
objAdapter1.Fill(objDataset1, "ExcelData");
objConn.Close();
}
catch (Exception ex)
{
throw ex;
}
return objDataset1;
}
The error message is
Assuming you don't need to write to the file, try adjusting your connection string to include the read only mode (Mode=Read). I have that in all of mine (where I don't need to write to the file) and I've never had a problem reading from workbooks that are already open:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
path + ";Mode=Read;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
I also don't tend to read Excel files as XML, so the Extended Properties for my connection strings are Excel 12.0;HDR=YES;IMEX=1;
I am working on a webform which has a file upload and a button and a gridview. user can upload excel file to database and also see preview in gridview .It is workin fine.
my code is
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/Publisher/ExcelFiles/") + filename);
DataSet ds = new DataSet();
string path = #"~/Publisher/ExcelFiles/" + filename;
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand myComm = new OleDbCommand("select * from [GIRLS$] ", myCon);
OleDbDataAdapter da = new OleDbDataAdapter(myComm);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
but my problem is path of excel file it gives me an error at da.Fill(ds);
'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Publisher\ExcelFiles\glist.xls' 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.
excel file resides in my website directory. This error don't come if I changed the path to
string path= #"D:\Vikas Rana\New folder (4)\glist.xls";
any help will be appreciated.
Thanks in advance.
try this
string filename = Path.GetFileName(FileUpload1.FileName);
DataSet ds = new DataSet();
string path = Server.MapPath("~/Publisher/ExcelFiles/" + filename);
FileUpload1.SaveAs(path);
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;
DataSource=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand myComm = new OleDbCommand("select * from [GIRLS$] ", myCon);
OleDbDataAdapter da = new OleDbDataAdapter(myComm);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
if you upload .xls file use this
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path(which you prefer);Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"
if you upload .xlsx file use this
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=path(which you prefer);Extended Properties="Excel 12.0;HDR=YES;IMEX=1;"
I mean
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;
DataSource=" + path + ";Extended Properties=Excel 8.0;HDR=YES;IMEX=1;");
Hi there I am trying to establish a connection to a data source and extract the information and display it in a grid view. The problem is that i always get null value for ada. Is it possible to have mistyped the query or is there something wrong with the adapter?
Furthermore I am using the myInt variable to insert different data sources because i hav to process more than one file, maybe this could be problematic as well.
try
{
//establish connectioin
OleDbConnection conn = new OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + myInt + ";" + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT * FROM MarkingSheet$]", conn);
DataSet ds = new DataSet();
ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
conn.Close();
}
ANSWER
Thats what worked for me
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + myPath + ";Excel 12.0;HDR=YES;"); ;
conn.Open();
OleDbDataAdapter ada = new OleDbDataAdapter("select * from [Marking Sheet$]", conn); ;
DataSet ds = new DataSet();
ada.Fill(ds);
Change the sql to
"SELECT * FROM [MarkingSheet$]"
since there's a missing opening bracket.
The Fault in your code is that you havent opened a connection when you attempt to fill the Adapter. Your SQL Statement is also wrong. You may also wan wish to bind the DataTable to the DataGridView too like this :-
try
{
OleDbConnection conn = new OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + myInt + ";" + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT * FROM [MarkingSheet$]", conn);
DataSet ds = new DataSet();
conn.Open();
ada.Fill(ds.Tables[0]);
conn.Close();
BindingSource bs = new BindingSource();
bs.Datasource = ds.Tables[0];
dataGridView1.DataSource = bs;
}
catch(OledbException x)
{
// Handle Exception
}
EDIT **
Try Changing your connection string to :-
string connString = "provider=Microsoft.Jet.OLEDB.4.0;Data source=" + myInt + ";Extended Properties=Excel 8.0;HDR=Yes;IMEX=1\";";