I am facing a problem while uploading an excel sheet using oledb. Couple of weeks ago, it was working perfectly so I moved to different module.
But when I am testing and debugging the application now it is throwing an error.
Someone please look into this as I am not able to resolve this issue, also, their is no resolution posted when I searched on google as well....
Error: DisconnectedContext was Detected
Transition into COM context 0x673dd0 for this RuntimeCallableWrapper failed with
the following error: The object invoked has disconnected from its clients.
(Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). This is typically
because the COM context 0x673dd0 where this RuntimeCallableWrapper was
created has been disconnected or it is busy doing something else. Releasing
the interfaces from the current COM context (COM context 0x673d18). This
may cause corruption or data loss. To avoid this problem, please ensure
that all COM contexts/apartments/threads stay alive and are available for
context transition, until the application is completely done with the
RuntimeCallableWrappers that represents COM components that live inside them.
My Code: For FileUpload
string filefullpath = null;
try
{
string filepath = Server.MapPath("~/BulkUploadFile/");
string filename = fuDatabaseFile.FileName;
string filetype = Path.GetExtension(filename);
string filenamewithoutextension = Path.GetFileNameWithoutExtension(filepath);
string append = DateTime.Now.ToString("yyyyMMddHHmmssfffFFF");
string newfilename = filenamewithoutextension + append + filetype;
filefullpath = filepath + newfilename;
fuDatabaseFile.SaveAs(filefullpath);
}
catch (Exception ex)
{
}
Code for ExcelConnectivity:
FileUpload();
string excelconnectionstring = #"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + FileUpload() + ";Extended Properties=Excel 12.0;Persist Security Info=False";
using (OleDbConnection excelcon = new OleDbConnection(excelconnectionstring))
{
using (OleDbCommand command = new OleDbCommand("", excelcon))
{
command.CommandText = "Select * from [Sheet1$]";
excelcon.Open();
using (OleDbDataReader reader = command.ExecuteReader())
{
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("EmpID");
}
}
}
Related
I am getting a System.AccessViolationException error when trying to use the .Open() method on my OleDbConnection variable. But, confoundingly, it doesn't seem to happen when I run my code on a different computer.
My code is for a service that I want running on a server. It appears to work correctly when I run it on my personal computer, but it throws the AccessViolationException error when I try running it on the server.
Code Versions:
I am writing in C# using Visual Studios 2019 on Windows 10
OleDbConnection is from "Assembly System.Data, Version=4.0.0.0"
ADOX is from "Assembly Interop.ADOX, Version=2.8.0.0", System.Runtime.InteropServices
ADODB is from "Assembly Interop.ADODB, Version=2.8.0.0", System.Runtime.InteropServices
My code:
internal static bool CreateMDB(MySchemaClass schema, string filePath)
{
OleDbConnection conn = null;
bool status = true;
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", filePath);
try
{
// Setup new file
catalog.Create(connectionString);
((ADODB.Connection)catalog.ActiveConnection).Close();
conn = new OleDbConnection(connectionString);
conn.Open(); // <-- Error occurs here
// Write to new file
WriteDataToFile(schema, conn);
}
catch (Exception ex)
{
status = false;
}
finally
{
if (conn != null)
conn.Close();
}
return status;
}
StackTrace:
The best lead I have found so far is this post, but I'm a bit hazy about how to proceed from that starting point.
To programmatically create an Access database do the following:
Add reference to Microsoft ADO Ext. 6.0 for DDL and Security
In VS menu, click Project
Select Add Reference
Click COM
Check Microsoft ADO Ext. 6.0 for DDL and Security
CreateAccessDatabase
public static string CreateAccessDatabase(string fullyQualifiedAccessFilename, string dbPassword = "")
{
string connectionString = String.Format(#"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}", fullyQualifiedAccessFilename);
if (String.IsNullOrEmpty(fullyQualifiedAccessFilename))
{
throw new Exception("Error (CreateAccessDatabase) - Database filename is null or empty.");
}
if (!String.IsNullOrEmpty(dbPassword))
{
connectionString = String.Format(#"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0};Jet OLEDB:Database Password='{1}'", fullyQualifiedAccessFilename, dbPassword);
}
//create new instance
ADOX.Catalog cat = new ADOX.Catalog();
//create Access database
cat.Create(connectionString);
//close connection
cat.ActiveConnection.Close();
//release COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
GC.Collect();
cat = null;
return String.Format("Database created '{0}'", fullyQualifiedAccessFilename);
}
Usage:
string result = string.Empty;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Access Database 2000-2003 (*.mdb)|*.mdb|Access Database 2007 (*.accdb)|*.accdb";
if (sfd.ShowDialog() == DialogResult.OK)
{
//create Access database
result = CreateAccessDatabase(sfd.FileName);
}
To programmatically create a table in an Access database:
Add using statement:
using System.Data.OleDb;
CreateTableProduct:
public static int CreateTableProduct(string fullyQualifiedAccessFilename, string dbPassword = "")
{
int result = 0;
string connectionString = String.Format(#"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}", fullyQualifiedAccessFilename);
if (!String.IsNullOrEmpty(dbPassword))
{
connectionString = String.Format(#"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0};Jet OLEDB:Database Password='{1}'", fullyQualifiedAccessFilename, dbPassword);
}
string sqlText = string.Empty;
sqlText = "CREATE TABLE Product ";
sqlText += "(ID AUTOINCREMENT not null primary key,";
sqlText += " Name varchar(50) not null,";
sqlText += " Price currency, Quantity integer);";
using (OleDbConnection con = new OleDbConnection(connectionString))
{
//open connection
con.Open();
using (OleDbCommand sqlCmd = new OleDbCommand(sqlText, con))
{
//execute command
result = sqlCmd.ExecuteNonQuery();
}
}
return result;
}
Resources:
ADO Features for each Release
Which Access file format should I use?
CREATE TABLE statement (Microsoft Access SQL)
System.Data.OleDb Namespace
The underwhelming answer to the problem is that my server was lacking some of the files my personal computer had, and only needed the correct files installed.
The missing piece in this case was the Microsoft Access Database Engine 2016 Redistributable, which I ended up finding here. Running that executable on my server got the needed files and everything worked after that.
We have an application where the data in Excel file (present in shared path) moves to Database. In case of any error, the files moves to error folder by writing the error in a log file.It uses a windows service for the operation.
Sometimes the file doesn't have any error still moves to error folder by writing log External table is not in the expected format. But the same file uploading again for once or multiple times, its moving to Database without any errors.
The windows service, DB and shared path are present in XP Server. Application was running fine all these years. But in the recent days, above mentioned problem is occurring for almost every file.
We have installed Microsoft 2003, 2007,2012 office components and access engines too. But still the issue still persists.
I am mentioning the Windows service code below. Pls help. Thanks in advance.
using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;
namespace Impexp_Service
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer T1 = new System.Timers.Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
///start
///
{
SqlConnection strconnection = new SqlConnection();
strconnection.ConnectionString = #"Data Source=XXXXXX;Initial Catalog=XXXX;User ID=XX;Password=XXXXXX;";
strconnection.Open();
// To get the all files placed at the shared path
DirectoryInfo directory = new DirectoryInfo(#"D:\Impexp\Data\");
FileInfo[] files = directory.GetFiles("*.xlsx");
foreach (var f in files)
{
string path = f.FullName;
// TO establish connection to the excel sheet
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);
DbDataReader dr = cmd.ExecuteReader();
// OleDbDataReader dReader;
// dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "imp_master_test";
sqlBulk.WriteToServer(dr);
excelConnection.Close();
File.Delete(path);
// To move error files to the error folder
/// end
T1.Interval = 20000;
T1.Enabled = true;
T1.Start();
T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
}
}
}
void T1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
T1.Enabled = false;
try
{
SqlConnection strconnection = new SqlConnection();
strconnection.ConnectionString = #"Data Source=10.91.XXXXXX;Initial Catalog=XXXXX;User ID=XXXXX;Password=XXXXX;";
strconnection.Open();
// To get the all files placed at the shared path
DirectoryInfo directory = new DirectoryInfo(#"D:\Impexp\Data\");
FileInfo[] files = directory.GetFiles("*.xlsx");
foreach (var f in files)
{
string path = f.FullName;
// TO establish connection to the excel sheet
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
try
{
excelConnection.Open();
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);
DbDataReader dr = cmd.ExecuteReader();
// OleDbDataReader dReader;
// dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "imp_master_prod";
sqlBulk.WriteToServer(dr);
excelConnection.Close();
File.Delete(path);
}
// To move error files to the error folder
catch (Exception exp)
{
excelConnection.Close();
File.Move(path, Path.Combine(#"D:\Impexp\error\", f.Name));
string path1 = #"D:\Impexp\error\error.txt";
if (File.Exists(path1))
{
// Create a file to write to.
using (StreamWriter sw = File.AppendText(path1))
{
sw.WriteLine("File : " + path + " : " + exp.Message);
sw.Flush();
}
}
T1.Enabled = true;
T1.Start();
}
}
strconnection.Close();
// End of TRY 1
}
catch (UnauthorizedAccessException UAEx)
{
string path1 = #"D:\Impexp\error\error.txt";
if (File.Exists(path1))
{
// Create a file to write to.
using (StreamWriter sw = File.AppendText(path1))
{
sw.WriteLine(UAEx.Message);
sw.Flush();
}
}
T1.Enabled = true;
T1.Start();
}
catch (PathTooLongException PathEx)
{
string path1 = #"D:\Impexp\error\error.txt";
if (File.Exists(path1))
{
// Create a file to write to.
using (StreamWriter sw = File.AppendText(path1))
{
sw.WriteLine(PathEx.Message);
sw.Flush();
}
}
T1.Enabled = true;
T1.Start();
}
T1.Enabled = true;
T1.Start();
}
protected override void OnStop()
{
}
}
}
I did some searching regarding the OLEDB coms and newer versions of Excel. It seems as though a great many people are having compatibility issues with them.
Unfortunately, it doesn't look like Microsoft is giving this any attention. Microsoft announced the depreciation of OLEDB functionality many years ago, and they have stopped adding any kind of internal support in their Office products and SQL servers. In fact,
The official shutdown date for MSAccess Web Apps and Web Databases was April 2018. That being the case, an update to the server, the client version of windows, or the client version of Excel may have triggered this, and it doesn't look like there is going to be a fix. I myself have started using 3rd party packages (free ones are available) to handle the interop with office products because I was tired of banging my head against the wall to create workarounds. Honestly, I don't know why Access still exists if they are taking away the ability to programmatically connect to an Access database.
I know this doesn't fix your problem, but it's better to face the truth and move on than to try to fix something that isn't going to fix.
Looking at this question, this appears to be an issue with reading the excel file, not the SQL table. Try changing the Excel connection string.
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
to
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=Yes;IMEX=1\";
Also, looking at another answer, the root cause could be uploading newer versions of excel.
Are you using an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0?
You can change the string to some other as below:
public static string path = #"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
OR
Change the connection string from the link below:
http://www.connectionstrings.com/excel-2007
I am attempting to query data from within an Excel sheet using my C# application. I seem to be able to connect to the file now but I am getting the following exception...
The Microsoft Office Access database engine could not find the object 'Sheet1$'.
I'm using a connection string similar to that of Excel connection strings
This is my code...
The code is basically a method (tucked away in a class) that uses a parameter variable to return a data from one column based on provided data in another....This same method works for SQL connections in my other applications..this is my first swing at an excel sheet however..
public static string ReturnDefinition(string remCode)
{
string strReturnMessage = "";
string excelConnectString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MEDICARE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""";
OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand objCmd = new OleDbCommand("Select * from [Sheet1$] where Code = #remCode", objConn);
objCmd.Parameters.AddWithValue("#remCode", remCode);
try
{
objConn.Open();
OleDbDataReader ExcelDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
if (ExcelDataReader.HasRows)
{
while (ExcelDataReader.Read())
{
if (string.IsNullOrEmpty((string)ExcelDataReader["Description"]))
{
strReturnMessage = "** ERROR **";
}
else
{
strReturnMessage = ExcelDataReader["Description"].ToString();
}
}
}
else
{
strReturnMessage = "** ERROR **";
}
ExcelDataReader.Close();
return strReturnMessage;
}
catch (Exception ex)
{
return "** ERROR **: " + ex.Message;
}
finally
{
}
}
5/30
I realize there is literature out there that covers connections to Excel using OLEDB but I think I've boiled the issue down to a read issue in the sheet its self. Again, this is the first time I've tried to connect to Excel. My HDR is set to true, as I intend to treat the sheet like a SQL table.
Changed Excel to v.14.0 in Extended Properties
I may have been targeting the wrong version of excel. Specifically, the excel sheet was created using Office 2010. Using This Article for reference, I changed the version from 12.0 to 14.0.
Now I get Could not find installable ISAM
Well, I think I've got it figured out for now.
At least I can get to the sheet anyway.
I'll just need to sort out whats going on with my data types.
I now get Data type mismatch in criteria expression.
The data being passed from my parameter variable is a string but the column name ("Code") I am trying to reach contains strings in some rows and int32 in others. If it were SQL I would say this column would be of type CHAR(whatever). I don't know in this case.
Anyway, here is my own resolution...it seems all I needed to do was assign the sheet name to a variable and then include that variable in my query.
enter public static string ReturnDefinition(string remCode)
{
string strReturnMessage = "";
string excelConnectString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MEDICARE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""";
string excelSheet = "Sheet1$";
OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand excelCmd = new OleDbCommand("Select * from ["+ excelSheet + "] where Code = #remCode", objConn);
excelCmd.Parameters.AddWithValue("#remCode", remCode);
try
{
objConn.Open();
OleDbDataReader ExcelDataReader = excelCmd.ExecuteReader(CommandBehavior.CloseConnection);
if (ExcelDataReader.HasRows)
{
while (ExcelDataReader.Read())
{
if (string.IsNullOrEmpty((string)ExcelDataReader["Description"]))
{
strReturnMessage = "** ERROR **";
}
else
{
strReturnMessage = ExcelDataReader["Description"].ToString();
}
}
}
else
{
strReturnMessage = "** ERROR **";
}
ExcelDataReader.Close();
return strReturnMessage;
}
catch (Exception ex)
{
return "** ERROR **: " + ex.Message;
}
finally
{
}
}
While inserting the data from excel to database it throwing the above error
Here is my code:
void insertDB()
{
string FileName = lblFileName.Text;
string Extension = Path.GetExtension(FileName);
string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FolderPath + FileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
break;
case ".xlsx": //Excel 07
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FolderPath + FileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
break;
}
try
{
using (var context = new LQTransAgentSeaFreightRateDataContext())
{
string sql = string.Format("Select * FROM [{0}]" , ddlSheets.SelectedValue);
using (var myConnection = new OleDbConnection(conStr))
using (var myCommand = new OleDbCommand(sql, myConnection))
{
myConnection.Open();
var myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
context.TB_TransAgentSeaFreightRates.InsertOnSubmit(new TB_TransAgentSeaFreightRate()
{
tASF_VCPOD = myReader.GetString(0),
tASF_VCPOL = myReader.GetString(1),
tASF_VCForwarder = myReader.GetString(2),
tASF_VCForwarderReference = myReader.GetString(3),
tASF_VCShippingLine = myReader.GetString(4),
tASF_VCContainerType = myReader.GetString(5),
tASF_VCContainerSize = myReader.GetString(6),
tASF_DTEValidFrom = Convert.ToDateTime(myReader.GetString(7)),
tASF_DTEValidTo = Convert.ToDateTime(myReader.GetString(8)),
tASF_NUBasicRate = mobjGenlib.ConvertLong(myReader.GetString(9)),
tASF_NUPAF = mobjGenlib.ConvertLong(myReader.GetString(10)),
tASF_NUCAF = mobjGenlib.ConvertLong(myReader.GetString(11)),
tASF_NUPSS = mobjGenlib.ConvertLong(myReader.GetString(12)),
tASF_NUTotalAmount = mobjGenlib.ConvertLong(myReader.GetString(13)),
tASF_NUFreeDays = mobjGenlib.ConvertLong(myReader.GetString(14)),
tASF_VCCreditDays = myReader.GetString(15),
tASF_VCNITDeposit = myReader.GetString(16),
tASF_NUIsActive = 1,
tASF_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text)
});
}
}
context.SubmitChanges();
}
}
catch (Exception ex)
{
lblMessage.ForeColor = System.Drawing.Color.White;
lblMessage.Text = ex.Message;
}
}
I don't have an idea to how to make it to work fine...if any one suggest me it would be very helpful.
Thanks in adavance.
The error was caused by improper unboxing. (Ref:
http://msdn.microsoft.com/en-us/library/b95fkada(v=vs.80).aspx)
Response Limitations: I don't know the property types for
TB_TransAgentSeaFreightRate.
Assumption: The types of the data being assigned to the properties matches the types of
the properties.
There are 2 ways, that I see, of handling this from a debugging
standpoint
Given the way your are doing this, you won't be able to see the exact
failure. You could temporarily move the initialization of the
members out of the new statement.
Alternatively, you could debug through the code repetitively each
time commenting out 1 line from the assignments until you have found
the offending line.
I am discounting Convert.ToDateTime as the source of the problem. It throws a
FormatException for and invalid DateTime.
Ref. http://msdn.microsoft.com/en-us/library/xhz1w05e(v=vs.110).aspx
My guess is that it is occurring on one of the mobjGenlib.ConvertLong. You're going to
have to debug through the code to find the line in question. Also keep in mind that this error is happening because of the data. You will need to account for this type of error, and deal with it in the code.
I'm trying to copy data from excel to sql server but facing the following error.
The Microsoft Office 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.
My code is:
protected void importdatafromexcel(string filepath)
{
string sqltable = "PFDummyExcel";
string exceldataquery = "select EmployeeId,EmployeeName,Amount from [Sheet1$]";
string excelconnectionstring = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0;Persist Security Info=False";
string sqlconnectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["HRGold"].ConnectionString;
SqlConnection con = new SqlConnection(sqlconnectionstring);
OleDbConnection oledb = new OleDbConnection(excelconnectionstring);
OleDbCommand oledbcmd = new OleDbCommand(exceldataquery, oledb);
oledb.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
bulkcopy.DestinationTableName = sqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
oledb.Close();
}
Please tell me how i solve this..
This error is raised because of you are trying to access sheet (which name is sheet1) in excel file. By default first sheet name is "sheet1" but user have either rename this name or delete this sheet.
To resolved this issue first of all you have to get all sheet name from excel file, then you have to pass this sheet name in your above code to import data.
string filePath = "your file path";
string excelconnectionstring = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0;Persist Security Info=False";
OleDbConnection Connection = new OleDbConnection(excelconnectionstring);
DataTable activityDataTable = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if(activityDataTable != null)
{
//validate worksheet name.
var itemsOfWorksheet = new List<SelectListItem>();
string worksheetName;
for (int cnt = 0; cnt < activityDataTable.Rows.Count; cnt++)
{
worksheetName = activityDataTable.Rows[cnt]["TABLE_NAME"].ToString();
if (worksheetName.Contains('\''))
{
worksheetName = worksheetName.Replace('\'', ' ').Trim();
}
if (worksheetName.Trim().EndsWith("$"))
itemsOfWorksheet.Add(new SelectListItem { Text = worksheetName.TrimEnd('$'), Value = worksheetName });
}
}
// itemsOfWorksheet : all worksheet name is added in this
so you can use itemsOfWorksheet[0] as sheet name in-place of "sheet1"
I had similar issue, I sorted it out by
Saving the excel file from fileuploader to a temporary folder inside website folder.
Using path to that file in my connection string
Rest all was same and now the error: The Microsoft Office Access database engine could not find the object 'sheet1$' was gone.