471,317 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Problem with OleDbTransaction with Header and Itens tables

Hi,

I'm with the folowing problem:

I have two tables: header(id_header) and Itens(id_header, id_item).

When I insert data in Itens table, this error is returned: "You cannot add
or change a record because a related record is required in table 'header'."

I'm using MS Access database.

See the code:

OleDbConnection connection = new
OleDbConnection(DBConnection.GetConnectionString() );
OleDbTransaction transaction = null;
OleDbCommand command = new OleDbCommand();

command.Connection = connection;

connection.Open();

transaction = connection.BeginTransaction();
command.Transaction = transaction;

try
{
sql = "insert into header(id_header) values(@header)";

OleDbParameter[] oleDbParameter = { new
OleDbParameter("@header",id_header)};

command.Parameters.Clear();
for(int i=0; i<oleDbParameter.Length; i++)
command.Parameters.Add(oleDbParameter[i]);

command.CommandText = sql;
command.ExecuteNonQuery();
sql = "insert into item(id_header, id_item)
values(@header, @item)";

OleDbParameter[] oleDbParameter = { new
OleDbParameter("@header",id_header), new OleDbParameter("@item",id_item)};

command.Parameters.Clear();
for(int i=0; i<oleDbParameter.Length; i++)
command.Parameters.Add(oleDbParameter[i]);

command.CommandText = sql;
command.ExecuteNonQuery();

transaction.Commit();
}
....

Thanks.
Dec 2 '05 #1
0 992

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Raphaël Désalbres | last post: by
4 posts views Thread by Raphaël Désalbres | last post: by
1 post views Thread by Marco Roberto | last post: by
1 post views Thread by John | last post: by
3 posts views Thread by no | last post: by
7 posts views Thread by Kamal | last post: by
2 posts views Thread by arnabit | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.