Import excel error after deploy application - c#

please need your help .
in my application i am using the file upload control to import excel file into database. when i am running the application in localhost it is working fine, but after deploying the application when i am running the application from same or other machine, the file upload control is not working and get the below error.
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Uploading code :
if (FileUpload.HasFile)
{
string path = string.Concat((Server.MapPath("~/Temp/" + FileUpload.FileName)));
FileUpload.PostedFile.SaveAs(path);
OleDbConnection OleDbCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", OleDbCon);
OleDbDataAdapter objAdapter = new OleDbDataAdapter(cmd);
OleDbCon.Open();
DbDataReader dr = cmd.ExecuteReader();
string con_str = ConfigurationManager.ConnectionStrings[1].ConnectionString;
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "[dbo].[HP_temp]";
bulkInsert.WriteToServer(dr);
OleDbCon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/Temp/"))), File.Delete);
Label_UploadMsg.ForeColor = Color.Green;
Label_UploadMsg.Text = "Imported sucess";
}
else
{
Label_UploadMsg.ForeColor = Color.Red;
Label_UploadMsg.Text = "Error";
}

Install the Microsoft Access Database Engine 2010 Redistributable.
https://www.microsoft.com/en-gb/download/details.aspx?id=13255
Pay particular attention to the type of version (32/64 bit).
Reference: http://www.mikesdotnetting.com/article/280/solved-the-microsoft-ace-oledb-12-0-provider-is-not-registered-on-the-local-mac

Related

c# read Microsoft Edge History

I couldn't figure out how to get Edge history in c#.
I've tried this code:
DataSet ds = new DataSet();
string mySelectQuery = "SELECT * FROM " + "Container_n";
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + #"C:\Users\user\AppData\Local\Microsoft\Windows\WebCache\WebCacheV01.dat" + ";Extended Properties=\"text;HDR=YES;FMT=Delimited\"");
OleDbDataAdapter dsCmd = new OleDbDataAdapter(mySelectQuery, myConnection);
//Fill the DataSet object
dsCmd.Fill(ds, "Packets");
myConnection.Close();
But the row dsCmd.Fill(ds, "Packets"); throws:
The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local
machine.
I don't want to install it on every computer that I use the app on.
Is there any other way of doing it?

Error when trying to load Excel information

I have C# WinForms application that gets information from Excel sheet and stores it to my database, my code works fine on my PC but when I changed it an Error
System.InvalidOperationException: 'The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.'
display. I solved this problem by changing the project platform from x86 to x64 but when I launch the project with x64 I can't charge many files so I have to run it on x86. Guys any solutions?
Rq: My code:
String name = "Feuil1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
textBox_path.Text +
";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataview1.DataSource = data;
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["UR2k_CS.Properties.Settings.StoreConnectionString"].ConnectionString))
{
String query = "INSERT INTO dbo.Stock (RFID,name,refrence,prix,Stockdate) VALUES (#RFID,#name,#refrence,#prix,#Stockdate)";
connection.Open();
for (int i=0;i<dataview1.Rows.Count-1;i++)
{
using (SqlCommand command = new SqlCommand(query, connection))
{
if ((dataview1.Rows[i].Cells[0].Value.ToString()!=null)&&((!checkifexist(dataview1.Rows[i].Cells[0].Value.ToString()))) &&(dataview1.Rows[i].Cells[0].Value.ToString()!=""))
{
command.Parameters.AddWithValue("#RFID", dataview1.Rows[i].Cells[0].Value.ToString());
command.Parameters.AddWithValue("#name", dataview1.Rows[i].Cells[1].Value.ToString());
command.Parameters.AddWithValue("#refrence", dataview1.Rows[i].Cells[2].Value.ToString());
command.Parameters.AddWithValue("#prix", dataview1.Rows[i].Cells[3].Value.ToString());
command.Parameters.AddWithValue("#Stockdate", DateTime.Now);
command.ExecuteNonQuery();
}
}
}
connection.Close();
}
You need to download the 32 bit driver from here microsoft downloads (I think this is the correct one) but the difficulty you may run into is that a machine cannot have both the 64bit and 32bit versions installed side by side.
Using EPPlus is a more modern approach.

Oledb connection throws visual studio JIT compiler popup when hosted on IIS asp.net

I have a solution where i need to read values from Excel. I am using OLEDB connection to do the same. It all works fine when i run the solution in local. But when i host it in IIS i get the JIT compiler pop up when the oledb connection code is hit. Not able to figure out the problem.
Below is the OLEDB connection code :
public static DataSet FirstColumn(string filename)
{
string filepath = filename;
string sheetName = "First";
//Oledb connection to generate excel in temporary folder on server
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filepath + "';Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
conn.Open();
OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter
("select * from [" + sheetName + "$]", conn);
DataSet excelDataSet = new DataSet();
objDA.Fill(excelDataSet);
return excelDataSet;
}
The popup has an option to Debug or continue. When i debug i get "An unhandled win32 exception occurred in w3wp.exe"
Has anyone faced this issue?

The Microsoft.ACE.OLEDB.12.0 provider IS registered on the local machine but keep getting this error

I know this question has been asked plenty but I can't find a fix. I am trying to import an excel file and bind it to an aspxgridview. I have installed the AccessDatabaseEngine_X64.exe redistributable on my local machine, but I keep geeting the "The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine". below is a snippet of code.
string connStr = "";
string ext = Path.GetExtension(fuImportPP.FileName).ToLower();
string path = Server.MapPath("~/ExcelToGrid/" + fuImportPP.FileName);
gv = new GridView();
fuImportPP.SaveAs(path);
//if (ext.Trim() == ".xls")
// //connection string for that file which extantion is .xls
// connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
//else if (ext.Trim() == ".xlsx")
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
string query = "SELECT * FROM [GridViewExport$]";
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gv.DataSource = ds.Tables[0];
gv.DataBind();
phGridViewHolder.Controls.Add(gv);
conn.Close();
First check the bitness of your office installation
Since you're using c#, Try and go to Project > Project Properties > Click on the Build Tab > Where it Platform Target > Change it so that for example if you Office Installation is 32 Bit then change it to 32 Bit and if it is 64 Bit then change it to 64 Bit.
After that save and try running your project again.

oledb connection error loading datatable but only on one machine

I'm at a loss. I have this code which runs fine on my machine and loads a csv to a datatable:
public static DataTable GetDataTableFromCsv(string path, string sheetname, string header)
{
var directory = new DirectoryInfo(path);
var myFile = directory.GetFiles().Where(x => x.Name.ToLowerInvariant().Contains(sheetname.ToLowerInvariant())).OrderByDescending(f => f.LastWriteTime).First().Name;
string sql = #"SELECT * FROM [" + myfile+ "]";
using (OleDbConnection connection = new OleDbConnection(
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.InvariantCulture;
adapter.Fill(dataTable);
return dataTable;
}
}
this works fine on my machine. On a different machine it does not though. I googled and checked we have the identical version of MSJet40.dll in the same place, we are both on 64bit machines but on the machine where it doesn't work I don't have Visual studio so I can't debug.

Categories

Resources