A pair of more related databases How to add records in VB.NET

zhaozj2021-02-16  55

There are two tables Order (ORDERID, ORDERNAME, TOTALPRICE), ORDERDETAIL (ORDERID, PRICUCTID, PRODUCTNAME, Price) have established a pair of multi-associated (ORDERID). Create a form with VB.NET, used for primary tables and schedules Data entry and update. Add two SQLDATADAPTERs on the form, select the Order and OrderDetail tables, and generate a dataset, create associations in the DataSet (ORDERID)

In the primary table: ORDERID, ORDERNAME Binds in two TextBox text boxes for accepting user input, Totalprice accepts summary values ​​of Price in the schedule. Mingfang table Binds to a DataGrid control, DataSource: dsorder; DataMember: ORDER . LeDERORDETAIL established associated name

problem:

Add a record in the primary table. After the user enters the ORDERID and ORDERNAME, enter the entry of the schedule, and the user enters ProductID, ProductName, the pricttion, prompts

"Detailed ORDERID column does not allow null values, do you want to correct this value?" What is the reason? Detail ORDERID for connecting the primary table If you enter the same ORDERID as the main table, prompt "ForeignKeyConstRAINT requires a sub-key value in the crosstab. Do you want to correct this value? "

Only after entering the OrderID and ORDERNAME entry in the primary table, update the primary table data source first, you can edit the update schedule, why?

Excuse me, have other better ways, achieve new records of primary and tomorrow tables at the same time?

thanks for your care!

Netizen: Fang Yu

Reply: http://qsltz@21cn.com

http: //qsl_tz@hotmail.com

Attaching some source code: Public Class fmOrders Inherits System.Windows.Forms.Form Dim WithEvents dtOrderDetails As New DataTable () Dim BmOrders As BindingManagerBase Dim BmOrderDetails As BindingManagerBase Dim OrderDetailsHasChange As Boolean Dim orderid As DataColumndim ordername as datacolumn Dim SubTotal As DataColumn

Private Sub bnFirst_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnFirst.Click BmOrders.Position = 0 End Sub Private Sub bnPrior_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnPrior. Click BmOrders.Position - = 1 End Sub Private Sub bnNext_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnNext.Click BmOrders.Position = 1 End Sub Private Sub bnLast_Click (ByVal sender As System.Object, Byval e as system.eventargs) Handles Bnlast.click Bmorders.position = Bmorders.count - 1 End Sub

Private Sub bnAdd_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnAdd.Click Try BmOrders.EndCurrentEdit () BmOrders.AddNew () Catch err As System.SystemException MessageBox.Show (err.ToString) End Try End Sub Private Sub bnLoad_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnLoad.Click fillData () End SubPrivate Sub bnUpdate_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnUpdate.Click Dim DSTestRDERSCHANGE AS Dataset

BmOrderDetails.EndCurrentEdit () BmOrders.EndCurrentEdit () If Not dsTestOrders.HasChanges (DataRowState.Deleted) Then Try daOrders.Update (dsTestOrders, "Orders") daOrderDetails.Update (dsTestOrders, "OrderDetails") dsTestOrders.AcceptChanges () Catch err As System.SystemException dsTestOrders.RejectChanges () MessageBox.Show (err.ToString) Throw End Try Else Try daOrderDetails.Update (dsTestOrders, "OrderDetails") daOrders.Update (dsTestOrders, "Orders") dsTestOrders.AcceptChanges () Catch err As System .Stemexception dstestorders.rejectchange () Messagebox.show (Err.Tostring) throw end end if End Sub

Private Sub fmOrders_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load FillData () dtOrderDetails = dsTestOrders.Tables ( "OrderDetails") BmOrders = Me.BindingContext (dsTestOrders, "Orders") BmOrderDetails = Me .BindingContext (dsTestOrders, "Orders.OrdersOrderDetails") OrderDetailsHasChange = True SubTotal = dsTestOrders.Tables ( "Orders"). Columns ( "SubTotal") SubTotal.DefaultValue = 0 Price = dsTestOrders.Tables ( "OrderDetails"). Columns ( " Price ") Price.DefaultValue = 0 End SubPrivate Sub fillData () dsTestOrders.EnforceConstraints = False daOrders.Fill (dsTestOrders) daOrderDetails.Fill (dsTestOrders) dsTestOrders.EnforceConstraints = True End Sub Private Sub dtOrderDetails_ColumnChanged (ByVal sender As Object, ByVal e As System.Data.DatacolumnChangeeventArgs_ handles dtorderdetails.columnchanged

Dim drOrders As DataRow Dim drOrderDetails As DataRow Dim iSubTotal As Integer Try If OrderDetailsHasChange Then OrderDetailsHasChange = False drOrders = dsTestOrders.Tables ( "Orders"). Rows (BmOrders.Position) iSubTotal = 0 For Each drOrderDetails In drOrders.GetChildRows ( "OrdersOrderDetails" )

Isubtotal = isubtotal drorderDetails ("price")

NEXT

DRORDERS.BEGINEDIT ()

DRORDERS ("Subtotal" = Isubtotal

DRORDERS.Endedit ()

END IF

Finally

ORDERDETAILSHASCHANGE = TRUE

END TRY

End Sub

Private Sub Bndelete_Click (Byval e as system.EventArgs) Handles Bndelete.Clickif Bmorders.count> 0 THEN

Bmorders.removeat (bmorders.position)

END IF

End Sub

Private sub Bncancel_Click (Byval e as system.Object, byval e as system.eventargs) Handles Bncancel.click

Bmorders.cancelcurrentedIt ()

End Sub

END CLASS

转载请注明原文地址:https://www.9cbs.com/read-23622.html

New Post(0)