write to excel using C#
hello, i'am doing a tutorial i found on the microsoft website where i'm simply suppose to write a couple of values into a table in excel. well my program builds fine but i have run through a couple of exceptions that i'am having a hard time fixing. here is the script:
using
using
System.Text;using
System.Reflection;namespace
myApp{
///<summary>
/// Summary description for Class1.
///</summary>
class Class1
{
staticvoid Main(string[] args)
{
object path = "C:\\Documents and Settings\\LKaba\\Desktop\\";
System.Data.OleDb.OleDbConnection objConn =new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
"Capitals.xls;Extended Properties=Excel 8.0;HDR = YES;");
objConn.Open();
// Add two records to the table named 'MyTable'.
System.Data.OleDb.OleDbCommand objCmd =new System.Data.OleDb.OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandText = "Insert into Sheet1 (City, State)" +
" values ('Abidjan', 'Ivory Coast')";
try
{
objCmd.ExecuteNonQuery();
}
catch(Exception error){
Console.WriteLine(error.StackTrace);
Console.WriteLine(error.Message);
}
objCmd.CommandText = "Insert into Sheet1 (City, State)" +
" values ('Bouake', 'Ivory Coast')";
objCmd.ExecuteNonQuery();
// Close the connection.objConn.Close();
}
}
}
the exception message is : :"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll"
and the execution breaks at the line in bold in the previous code "System.Data.OleDb.OleDbCommand objCmd =new System.Data.OleDb.OleDbCommand();"
i have been trying to fix for a couple of hours now...any hints.
thanks

