unhandled exception Reading data from Any Excel - c#

how can I go around this error
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: The Microsoft Jet database engine could not find the object Sheet1. Make sure the object exists and that you spell its name and the path name correctly"
on this code
using System.Data.OleDb;
String sConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= Book1;" + "Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1]",objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
objConn.Close();

Try to use [Sheet1$] instead of [Sheet1].

Related

How to read .xltx (Excel template) in C#

I am using OleDB connection for importing .xltx and it is showing exception - "External table not in expected format"
System.Data.DataTable dtExcel = new System.Data.DataTable();
string filePath = #"C:\Template.xltx";
string xlConnStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1;Format=xltx'";
System.Data.OleDb.OleDbConnection cxlConn = new OleDbConnection(xlConnStr);
string query = "Select * from [Sheet2$A3:C34]";
OleDbDataAdapter dtAdapter = new OleDbDataAdapter(query, cxlConn);
dtAdapter.Fill(dtExcel);
I am using Microsoft.Jet.OLEDB.12.0 and Extended Properties=Excel 12.0
Can anyone please help me?
If not Oledb, any other way to read .xltx into datatable?
dtExcel is datatable, the line at which exception thrown is dtAdapter.Fill(dtExcel);

Data type mismatch in criteria expression. MS AccessDB (cmd:ExecuteNonQuery())

Trying to update data in AccessDB using following code.
OleDbConnection conn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Saumil\Projects\REV-OEM\LoginInforREVOEM.accdb");
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * from ManualRunSettings", conn);
DataSet dss = new DataSet();
da.Fill(dss, "ManualRunSettings");
//Pump1
OleDbCommand cmd = new OleDbCommand("UPDATE ManualRunSettings SET [Pump1RunAccording] = #Pump1RunAccording WHERE [ID] = '1'", conn);
cmd.Parameters.AddWithValue("#Pump1RunAccording", domainUpDown1.Text);
da.UpdateCommand = cmd;
da.UpdateCommand.ExecuteNonQuery();
I am having following error,
An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Additional information: Data type mismatch in criteria expression.
Can anyone lead to where the problem might be?
Thanks in advance.

ERROR:Make sure that it does not include invalid characters or punctuation and that it is not too long

I try to get excel sheet names, with oledb.
My connection string is:
string ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
where filepath is a filename.
My code for this:
OleDbCommand cmd = new OleDbCommand(#"SELECT * FROM [Employee$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(Data);
Get an Error in this line
adapter.Fill(Data);
Error is
'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: 'Employee$' is not a valid name. Make sure
that it does not include invalid characters or punctuation and that it
is not too long.
How can this be Done?
Try this:
OleDbCommand cmd = new OleDbCommand(#"SELECT * FROM [dataGridView1_Data$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(Data);

Import Excel to Datagridview -

I want to Import Excel to Datagridview and after search found this: Import Excel to Datagridview
stirng file = "c:\myFile.xlsx";
DataGridView dgvIM;
private void Import()
{
String name = "Items";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
file +
";Extended Properties='Excel 8.0;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
System.Data.DataTable data = new System.Data.DataTable();
sda.Fill(data);
dgvIM.DataSource = data;
}
And received this error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: 'Items$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
What does it mean ?

SQLException was Unhandled. Viewing Excel File in GridView

I've been doing a C# project where I browse and view an Excel File into a gridview. However, there is an error message which I don't understand. Can someone please help me out on this.
This is the code I used:
private void buttonUpload_Click(object sender, EventArgs e)
{
string connectionString = String.Format(#"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text);
string query = String.Format("select * from [{0}$]", "Sheet1");
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
This is the error message:
"A network-related or instance-specific error occurred while establishing
a connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured
to allow remote connections.
(provider: SQL Network Interfaces,
error: 26 - Error Locating Server/Instance Specified)."
You are trying to connect to Excel via a SqlConnection.
To connect to Excel use OleDBConnection:
private void buttonUpload_Click(object sender, EventArgs e)
{
string connectionString = String.Format(#"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text)
var objConn = new OleDbConnection(connectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
objConn.Close();
}
You can not use SqlConnection or SqlDataAdapter for ODBC or JET connections. Use OleDbConnection or OleDbDataAdapter.
Please also make sure to dispose of connections and resources in a timely manner. Using the using statement is recommended here.
private void buttonUpload_Click(object sender, EventArgs e)
{
string connectionString = String.Format(#"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text);
DataSet objDataset1 = new DataSet();
using (OleDbConnection objConn = new OleDbConnection(connectionString))
{
objConn.Open();
using (OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn))
using (OleDbDataAdapter objAdapter1 = new OleDbDataAdapter())
{
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(objDataset1);
}
}
}

Categories

Resources