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 System.Data.Common;

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

[3590 byte] By [Becool] at [2007-12-25]
# 1

Is there any additional information being provided by the exception (such as in Message property) as to what is going wrong?

BrendanGrant at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2
honestly i dont even get any other information except the one i gave you. the console prompt is just blank. i tried to put a try and catch around the

"System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand();" to see if ti will give me more info but then all the following lines using the objCmd object cause a build failure. i dont know much about exception handling in .net may be iam just doing it wrong ..any more idea what i can do to egt some info about that exception? thanks

Becool at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 3

this is the full stack trace of the exception:

Could not find installable ISAM.
broke
at System.Data.OleDb.OleDbConnection.SetStateExecuting(OleDbCommand attempt,
String method, Boolean flag)
at System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String met
hod, Int32& localState)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behav
ior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at ConsoleApplication4.Class1.Main(String[] args) in c:\documents and setting
s\lkaba\desktop\app13\consoleapplication4\class1.cs:line 44
ExecuteNonQuery requires an open and available Connection. The connection's curr
ent state is Closed.

really need help on this.thanks

Becool at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 4

sorry here is the correct stacktrace: please ignore previous:

at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARA
MS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Ob
ject& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behav
ior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at ConsoleApplication4.Class1.Main(String[] args) in c:\documents and setting
s\lkaba\desktop\app13\consoleapplication4\class1.cs:line 45

the microsoft jet database could not find the Object 'Sheet1'..........

sheet1 is the page of the book.xls where my table is.

Becool at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 5

Ahh... that gives us a bit more information and a quick Google search says that many people have had the ‘Could not find installable ISAM’ error.

Lets try the first solution... adding a couple of extra quotes in part of the connection string... try changing it to:

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\";");

Does this work better for you?

BrendanGrant at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...