Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Connecting to excel datasheets using C#

I am attempting to write a program that takes Excel datasheet information and
puts it in a Access database.
I'm having problems.

My datasheet is called 'Baseline & Week1'

Here is the relevant code:

OleDbDataAdapter oleCmd = new OleDbDataAdapter("SELECT * FROM
[Baseline & Week1$]", oleConn);

.. . .

foreach(DataRow oleRow in oDS.Tables[0].Rows)
{
write some data
}

The above code works.

However, if I modify my foreach to look like this:

foreach(DataRow oleRow in oDS.Tables["[Baseline & Week1$]"].Rows),

substituting the actual name of my datasheet in place of '0', I get an
"Object reference not set to an instance of an object" error.

Is my ' oDS.Tables{"Baseline & Week1$]"] ' syntax incorrect?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Connecting to excel datasheets using C#

"Narwe" wrote ...

I am attempting to write a program that takes Excel datasheet information and
puts it in a Access database.
I'm having problems.

My datasheet is called 'Baseline & Week1'

Here is the relevant code:

OleDbDataAdapter oleCmd = new OleDbDataAdapter("SELECT * FROM
[Baseline & Week1$]", oleConn);

. . .

foreach(DataRow oleRow in oDS.Tables[0].Rows)
{
write some data
}

The above code works.

However, if I modify my foreach to look like this:

foreach(DataRow oleRow in oDS.Tables["[Baseline & Week1$]"].Rows),

substituting the actual name of my datasheet in place of '0', I get an
"Object reference not set to an instance of an object" error.

Is my ' oDS.Tables{"Baseline & Week1$]"] ' syntax incorrect?


Better to let the OLE DB provider handle the INSERT e.g.

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='C:\\Tempo\\db.xls';Extended Properties='Excel 8.0;HDR=YES'";
OleDbConnection Conn = new OleDbConnection(strConn);
Conn.Open();
string strSQL = "INSERT INTO
[Database=C:\\Tempo\\New_Jet_DB.mdb;].MyTable (MyKeyCol, MyIntCol)
SELECT * FROM ['Baseline & Week1$'];";
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
Comm.ExecuteNonQuery();
Comm.Connection.Close();

Jamie.

--
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting Excel 2007 to MySQL db? Abel MacAdam Excel Discussion (Misc queries) 0 July 29th 09 01:17 PM
Merge 2 datasheets into single Pivot Table. free_ur_mind Excel Worksheet Functions 2 June 25th 09 03:05 PM
Excel datasheet needs dividing into more datasheets arran tw Excel Discussion (Misc queries) 6 April 22nd 08 02:02 PM
I want to compare old and new datasheets and highlight changes Barry Excel Worksheet Functions 4 February 6th 08 06:33 PM
connecting wordpad and excel Jan Excel Discussion (Misc queries) 1 May 4th 07 01:37 PM


All times are GMT +1. The time now is 06:14 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"