Re: Adding a record using C# by Chris
Chris
Thu Dec 04 18:32:17 CST 2003
This is a multi-part message in MIME format.
------=_NextPart_000_0045_01C3BAD8.000E8640
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
There are a number of problems with this code.
1) Not serious, but there is no real need to manually add the FundData =
DataTable to the DataSet as the OleDbDataAdapter.Fill() will do this if =
it does not already exist in the DataTable
2) To propogate the DataSet changes back to the database through the =
OleDbDataAdapter you must supply the insert, update and delete commands. =
You can either do this manually or for the very simple cases you can use =
the OleDbCommandBuilder to automatically generate the commands.
eg.
System.Data.OleDb.OleDbDataAdapter AdpFunds =3D new =
System.Data.OleDb.OleDbDataAdapter(cmd1);
new System.Data.OleDb.OleDbCommandBuilder( AdpFunds );
That will enable the OleDbDataAdapter to infer the update, delete and =
insert commands from the SelectCommand that you supplied and the =
database meta data.=20
3) You must not call AcceptChanges before calling =
OleDbDataAdapter.Update(), this causes the DataSet records to be =
accepted and there status is set to be as if the records were already =
updated to the datastore so the OleDbDataAdapter.Update() will do =
nothing.
4) Finally the reason for your exception message. Well the easy =
sollution is to just add the table name to the OleDbDataAdapter.Update() =
call.
eg.
OleDbDataAdapter.Update( ds, "FundData" );
OR
You can setup a table mapping for the OleDbDataAdapter, that way you =
will not need to pass the table name to either the Fill() or the =
Update().
eg.
AdpFunds.TableMappings.Add( "Table", "FundData" );
This maps the default DataTable name "Table" to "FundData". Do this =
after creating the OleDbDataAdapter but before calling Fill() or =
Update().
I would recommend that you set the Insert, Update and Delete commands of =
the OleDbDataAdapter manually using parameters as this allows you more =
control and better performance. You can find more info about this in the =
MSDN.
Hope this helps
Chris Taylor =20
"Ben S" <benstrum@hotmail.com> wrote in message =
news:OHF7VXruDHA.2360@TK2MSFTNGP09.phx.gbl...
Can someone tell me what is wrong with this code? I am simply trying =
to add a record to a database. The error message is as follows:
An unhandled exception of type 'System.InvalidOperationException' =
occurred in system.data.dll
Additional information: Update unable to find TableMapping['Table'] or =
DataTable 'Table'.
**************************************************************
DataSet ds =3D new DataSet();
ds.Tables.Add("FundData");
// Create Access objects:=20
System.Data.OleDb.OleDbCommand cmd1 =3D new =
System.Data.OleDb.OleDbCommand("SELECT * FROM FundData", dbConn);
System.Data.OleDb.OleDbDataAdapter AdpFunds =3D new =
System.Data.OleDb.OleDbDataAdapter(cmd1);
AdpFunds.Fill(ds, "FundData");
DataRow dr =3D ds.Tables["FundData"].NewRow();
dr["Symbol"] =3D sym;
ds.Tables["FundData"].Rows.Add(dr);
ds.AcceptChanges();
AdpFunds.Update(ds);
------=_NextPart_000_0045_01C3BAD8.000E8640
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1264" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>There are a number of problems with =
this=20
code.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>1) Not serious, but there is no real =
need to=20
manually add the FundData DataTable to the DataSet as the=20
OleDbDataAdapter.Fill() will do this if it does not already exist in the =
DataTable</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>2) To propogate the DataSet changes =
back to the=20
database through the OleDbDataAdapter you must supply the insert, update =
and=20
delete commands. You can either do this manually or for the very simple =
cases=20
you can use the OleDbCommandBuilder to automatically generate the=20
commands.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> eg.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
System.Data.OleDb.OleDbDataAdapter AdpFunds =3D <FONT color=3D#0000ff=20
size=3D2>new</FONT><FONT size=3D2>=20
System.Data.OleDb.OleDbDataAdapter(cmd1);</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2> new =
System.Data.OleDb.OleDbCommandBuilder( AdpFunds );</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>That will enable =
the OleDbDataAdapter to infer=20
the update, delete and insert commands from the SelectCommand that you =
supplied=20
and the database meta data. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>3) You must not call AcceptChanges =
before calling=20
OleDbDataAdapter.Update(), this causes the DataSet records to be =
accepted and=20
there status is set to be as if the records were already updated to the=20
datastore so the OleDbDataAdapter.Update() will do nothing.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>4) Finally the reason for your =
exception message.=20
Well the easy sollution is to just add the table name to the=20
OleDbDataAdapter.Update() call.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> eg.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> OleDbDataAdapter.Update( =
ds,=20
"FundData" );</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>OR</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>You can setup a table mapping for the=20
OleDbDataAdapter, that way you will not need to pass the table name to =
either=20
the Fill() or the Update().</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> eg.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> AdpFunds.TableMappings.Add( =
"Table",=20
"FundData" );</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>This maps the default DataTable name =
"Table" to=20
"FundData". Do this after creating the OleDbDataAdapter but=20
before calling Fill() or Update().</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>I would recommend that you set the =
Insert, Update=20
and Delete commands of the OleDbDataAdapter manually using parameters as =
this=20
allows you more control and better performance. You can find more =
info=20
about this in the MSDN.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Hope this helps</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Chris Taylor =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Ben S" <<A=20
href=3D"mailto:benstrum@hotmail.com">benstrum@hotmail.com</A>> =
wrote in=20
message <A=20
=
href=3D"news:OHF7VXruDHA.2360@TK2MSFTNGP09.phx.gbl">news:OHF7VXruDHA.2360=
@TK2MSFTNGP09.phx.gbl</A>...</DIV>
<DIV><FONT size=3D2>
<P><FONT face=3DArial>Can someone tell me what is wrong with this =
code? I=20
am simply trying to add a record to a database. The error =
message is as=20
follows:</FONT></P>
<P><FONT face=3DArial>An unhandled exception of type=20
'System.InvalidOperationException' occurred in =
system.data.dll</FONT></P>
<P><FONT face=3DArial>Additional information: Update unable to find=20
TableMapping['Table'] or DataTable 'Table'.</FONT></P>
<P><FONT=20
=
face=3DArial>************************************************************=
**</FONT></P>
<P>DataSet ds =3D </FONT><FONT color=3D#0000ff =
size=3D2>new</FONT><FONT size=3D2>=20
DataSet();</FONT></P>
<P><FONT size=3D2>ds.Tables.Add("FundData");</P>
<P></FONT><FONT color=3D#008000 size=3D2>// Create Access objects:=20
</P></FONT><FONT size=3D2>
<P>System.Data.OleDb.OleDbCommand cmd1 =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>new</FONT><FONT size=3D2> =
System.Data.OleDb.OleDbCommand("SELECT * FROM=20
FundData", dbConn);</P>
<P>System.Data.OleDb.OleDbDataAdapter AdpFunds =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>new</FONT><FONT size=3D2> =
System.Data.OleDb.OleDbDataAdapter(cmd1);</P>
<P></P>
<P>AdpFunds.Fill(ds, "FundData");</P>
<P>DataRow dr =3D ds.Tables["FundData"].NewRow();</P>
<P>dr["Symbol"] =3D sym;</P>
<P>ds.Tables["FundData"].Rows.Add(dr);</P>
<P>ds.AcceptChanges();</P>
<P>AdpFunds.Update(ds);</P></FONT></DIV></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_0045_01C3BAD8.000E8640--