Error loading my DataTable into my Access database

J

Jeff

Hi,

I have been trying to load my dataTable in my database for 2 days
without any success. Any help would be appreciated.

INFO:

1- I am using OleDb.
2- I have a DataTable that I created manually from an array.
3- I also have a database with a table containing the exact same
column as the dataTable.
4- When I execute the following code I get an error saying: "Update
unable to find TableMapping['Table'] or DataTable 'Table'." I don't
understand why I would need mapping where both tables are identical.
5- All the example I found were where they initially pulled info from
the database first then update the database with changes. I didn't
find anything where you make your own datatable then update an
existing database.

I guess I must be forgetting something stupid, but before I jump from
window I thought I'd ask. ( don't worry I live on the main floor! )

thanks for your wisdom,

jf

CODE:

public void makeTable(DataTable dt, string tableName)
{
OleDbConnection conn = new OleDbConnection();

DataSet dset = new DataSet(tableName); //create the data set
dset.Tables.Add(dt); //add to collection

conn = db.getConnection();
if (conn.State == ConnectionState.Closed)
conn.Open();

OleDbDataAdapter myDataAdapter =
new OleDbDataAdapter("Select * from " +
tableName, conn);

//open the table
OleDbCommandBuilder olecb = new OleDbCommandBuilder(myDataAdapter);

myDataAdapter.Fill(dset, tableName);

try
{
myDataAdapter.Update(dset); //THIS LINE THROW ERROR!!!
conn.Close();
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
}
 
A

Andy Gaskell

try changing this line
myDataAdapter.Update(dset); //THIS LINE THROW ERROR!!!

to this
myDataAdapter.Update(dset, tableName); //THIS LINE THROW ERROR!!!
 
J

Jeff

I knew it was something simple, Thank you andy.

So to close the thread here is what I did.

I changed the line as proposed by Andy and it got rid of my error
message. But my data just wouldn't load in my database still. I had
forgot to name my dataTable. dt.TableName = tableName and that allowed
my data to load successfully. I also removed my open and close
connection statements as the dataAdapter open and close the connection
as needed.

Thanks again Andy, you triggered a nice chain of events.

jf


Andy Gaskell said:
try changing this line
myDataAdapter.Update(dset); //THIS LINE THROW ERROR!!!

to this
myDataAdapter.Update(dset, tableName); //THIS LINE THROW ERROR!!!

Jeff said:
Hi,

I have been trying to load my dataTable in my database for 2 days
without any success. Any help would be appreciated.

INFO:

1- I am using OleDb.
2- I have a DataTable that I created manually from an array.
3- I also have a database with a table containing the exact same
column as the dataTable.
4- When I execute the following code I get an error saying: "Update
unable to find TableMapping['Table'] or DataTable 'Table'." I don't
understand why I would need mapping where both tables are identical.
5- All the example I found were where they initially pulled info from
the database first then update the database with changes. I didn't
find anything where you make your own datatable then update an
existing database.

I guess I must be forgetting something stupid, but before I jump from
window I thought I'd ask. ( don't worry I live on the main floor! )

thanks for your wisdom,

jf

CODE:

public void makeTable(DataTable dt, string tableName)
{
OleDbConnection conn = new OleDbConnection();

DataSet dset = new DataSet(tableName); //create the data set
dset.Tables.Add(dt); //add to collection

conn = db.getConnection();
if (conn.State == ConnectionState.Closed)
conn.Open();

OleDbDataAdapter myDataAdapter =
new OleDbDataAdapter("Select * from " +
tableName, conn);

//open the table
OleDbCommandBuilder olecb = new OleDbCommandBuilder(myDataAdapter);

myDataAdapter.Fill(dset, tableName);

try
{
myDataAdapter.Update(dset); //THIS LINE THROW ERROR!!!
conn.Close();
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top