您无法添加或更改记录,因为表中需要相关记录 [英] You cannot add or change a record because a related record is required in table

查看:143
本文介绍了您无法添加或更改记录,因为表中需要相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用事务在两个表OrderDetails和CustOrder中插入值,但它给出了错误:您无法添加或更改记录,因为表中需要相关记录。

代码:

I am trying to insert value in two table OrderDetails And CustOrder using transaction but it gives me error: You cannot add or change a record because a related record is required in table.
Code :

private void FunInsOrderDetails()
        {
         using (OleDbConnection connection = new OleDbConnection(DataConnection.constring))
         {
          OleDbTransaction transaction = null;
           try
            {
              OleDbCommand command = connection.CreateCommand();                  
              connection.Open();
              transaction = connection.BeginTransaction(); // Assign Transaction to Command
              command.Transaction = transaction;
command.CommandText= "insert into Custorder values('" + cmbOrderType.Text + "'," + txtOrderNo.Text + ",'" + dtpDate.Value.ToString("yyyy-MM-dd") + "','" + txtYourOrderNo.Text + "'," + cmbCustName.SelectedValue.ToString() + ")";                  
                   command.ExecuteNonQuery();                   
                    FunInsOrder(command);
                    transaction.Commit();
                    connection.Close();
                }
                catch
                {
                    transaction.Rollback();
                    connection.Close();
                    throw;
                }
            }           
        }





插入第一个表后尝试从Custorder中获取OrderId table

使用查询:



After inserting into the first table try to fetch OrderId from Custorder table
using Query:

str="Select OrderId from Custorder where OrderNo="+txtOrderNo.Text+" AND OrderType='"+cmbOrderType.Text+"'";
              int OrderId = dc.RetutnID(str);



i想在OrderDetails中插入OrderId值表但它给了我一个错误:

您无法添加或更改记录,因为表中需要相关记录


i want to insert OrderId value in OrderDetails table but it gives me a error:
You cannot add or change a record because a related record is required in table

推荐答案

它是可能是您输入的数据未保存在数据库中,由于sql方面的某些错误,它可能会被回滚。在下一步中,您将获取订单ID键。
It is may be data you are entering is not saving in the database, due to some error at sql side it may be rolled back . and in next step you are fetching the order id key.


这篇关于您无法添加或更改记录,因为表中需要相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆