I’m trying to export some values in textboxes to several columns (Date, Temperature) in an excel sheet (.xlsx file). But it generates an error called “the INSERT INTO statement contains the following unknown field name: ‘Temperature'”. And when i remove the Temperature column from INSERT INTO statement, it works. Please help. I have given the codings beow:
private void button2_Click(object sender, EventArgs e)
{
openFileDialog2.ShowDialog();
string Path = openFileDialog2.FileName.Replace("'\'", "'\\'");
try
{
string szConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +Path+ ";Extended Properties='Excel 8.0;HDR=YES;'";
OleDbConnection conn = new OleDbConnection(szConn);
conn.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Weather Report$]([Date],[Temperature]) VALUES('" + textBox1.Text + "','" + textBox3.Text + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
Ensure that the Excel file in which you are trying to add data contains a header (First row) with these two values Date & Temperature.
Its trying to look for a Column with header value of Temperature in the Excel and is not able to find it…