By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

Problem with OleDbTransaction with Header and Itens tables

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.