帮助我解决父母的问题 [英] Help me with my parent\Child tables issue

查看:54
本文介绍了帮助我解决父母的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys

在我的Windows应用程序项目中,我正在使用数据集。有三张桌子:



产品

---------------

标题,身份证,价格



发票

---------------

ID,日期,描述



InvoiceItem

-------------- -

ID,Product_ID,Invoice_ID,Quantity



所有ID列都是Identity,tablename_ID格式列是外键。



在一个名为BuyForm的表单中,我得到所有输入,然后在Save_ButtonCLick中我想将这些信息插入到我提到的相关表中。

谷歌搜索我发现这篇文章:

插入关系使用DataSet和DataAdapter的数据

根据我从该文章中理解的内容,这是我的代码:

 DataRow invoice = DataSet .Tables [ 发票] NEWROW(); 
invoice [ Date] = DateConverter.ToPersianDate();
invoice [ 描述] = string .Empty;
DataSet.Tables [ Invoice]。Rows.Add(invoice);

DataRow product = null ;
DataRow invoiceItem = null ;
foreach (DataGridViewRow row in ProductGridView.Rows)
{
product = DataSet.Tables [ Product]。NewRow();
invoiceItem = DataSet.Tables [ InvoiceItem]。NewRow();

product [ Title] = row.Cells [ ProductColumn]。Value.ToString();
product [ BuyPrice] = row.Cells [ BuyPriceColumn]。值;
product [ SellPrice] = row.Cells [ SellPriceColumn]。值;
invoiceItem [ Count] = row.Cells [ CountColumn]。值;

invoiceItem.SetParentRow(invoice);
product.SetParentRow(发票);
DataSet.Tables [ Product]。Rows.Add(product);
DataSet.Tables [ InvoicItem]。Rows.Add(invoiceItem);

TableAdapterManager.UpdateAll(DataSet);
}



将invoiceItem添加到其DataTable时发生此错误:

 Column' Product_ID'不允许空值。



我应该如何更改我的代码?

谢谢

解决方案

您没有为InvoiceItem指定Product_Id并导致问题。 Identity字段仅用于主键,并且根据您的描述,Product_ID是外键,因此您必须以某种方式为其定义值。所以可能你应该有

 invoiceItem.SetParentRow(product); 



作为旁注

 product.SetParentRow(发票); 



看起来很奇怪,因为我怀疑发票是产品的母公司


Hello Guys
In my windows application project I am using Dataset. There are three tables:

Product
---------------
Title, ID, Price

Invoice
---------------
ID, Date, Description

InvoiceItem
---------------
ID, Product_ID, Invoice_ID, Quantity

All ID columnns are Identity and tablename_ID format columns are foreign Keys.

In a single form named BuyForm I get all inputs and then in a Save_ButtonCLick I want to Insert these information to the related tables I have mentioned.
After some googling I found this article:
Inserting relational data using DataSet and DataAdapter
Here is my code according to what I understand from that article:

DataRow invoice = DataSet.Tables["Invoice"].NewRow();
        invoice["Date"] = DateConverter.ToPersianDate();
        invoice["Description"] = string.Empty;
        DataSet.Tables["Invoice"].Rows.Add(invoice);
                    
        DataRow product = null;
        DataRow invoiceItem = null;
        foreach (DataGridViewRow row in ProductGridView.Rows)
        {
            product = DataSet.Tables["Product"].NewRow();
            invoiceItem = DataSet.Tables["InvoiceItem"].NewRow();
        
            product["Title"] = row.Cells["ProductColumn"].Value.ToString();
            product["BuyPrice"] = row.Cells["BuyPriceColumn"].Value;
            product["SellPrice"] = row.Cells["SellPriceColumn"].Value;
            invoiceItem["Count"] = row.Cells["CountColumn"].Value;
        
            invoiceItem.SetParentRow(invoice);
            product.SetParentRow(invoice);
            DataSet.Tables["Product"].Rows.Add(product);
            DataSet.Tables["InvoicItem"].Rows.Add(invoiceItem);
        
            TableAdapterManager.UpdateAll(DataSet);              
         }


This error occurs at time of adding invoiceItem to its DataTable:

Column 'Product_ID' does not allow nulls.


How should I change my code?
Thanks

解决方案

You don't specify the Product_Id for an InvoiceItem and that causes the problem. The Identity fields are used only for primary keys and based on you description, Product_ID is a foreign key so you must define the value for it, one way or another. So probably you should have

invoiceItem.SetParentRow(product);


As a side-note the row

product.SetParentRow(invoice);


looks odd since I doubt that invoice is a parent of product.


这篇关于帮助我解决父母的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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