Table Adapter Insert Help

PhillD

Well-known member
Joined
Mar 5, 2010
Messages
91
Programming Experience
10+
Can someone please help me.

What I am trying to do should be very simple but I can't figure it out.
I have a parent child form (Orders and Order Lines). I have 2 table adapters.
The Order details are in the first and the order lines are in the 2nd. When I save, I need to save a value from the order header into the lines. The order lines table adapter only lists text boxes that are bound to the order lines table adapter so I can't figure out how to pass in the value I need.

How could I do this?

Visual studio setup all the table adapters and binding sources etc...
 
Why exactly do you need to do that? If you're saving anything from the Orders into the OrderLines other than the key that relates them then you're duplicating data, which should generally be avoided.

That said, your Datatable/TableAdapter should handle all columns in the database table, which indicates that this value you want to save doesn't actually have a column in the OrderLines table. If there's no column then where are you trying to save it?

I think a more complete explanation is in order.
 
You hit the nail on the head! I need to insert the OrderID into the order lines table. The explanation.........

As you know from previous posts, I've been trying to find a way to use an identity field as the MainID in my form. I don’t want the user to see the ID until the record is saved (because it can change). I've posted this under several different scenarios (some of which you have replied to) but the premise is the same.

I don't really want to use GUID's either, I am just trying to achieve something I could do in MS Access and VB very easily.

I achieved my goal by turning off Enforce Constraints in the DataSet where it allows me to enter a record without specifying an OrderID, after I save, the OrderID is populated without error! Great, except when you also need a child element (like order lines). The problem is, when I create the order header, there is no OrderID yet, so when I start populating the child grid, it also has no associated OrderID. When I call the save function, an OrderID is generated for the order header, but the OrderID in the OrderLines is obviously still blank. So when the order lines get inserted, it errors out.

What I need to do is after the order header is saved, get the OrderID and make the OrderLines INSERT refer to the OrderID from the header.

It seems like this kind of functionality would be second place in VB but yet, I don't get many responses. Am I going about this the wrong way? I don't want to confuse my users by giving them ID's that change. Do you know what I mean by that? If 2 users create a record at the same time in the application, they will be given the same ID number. The first person to insert will keep their ID, the second user will not. If that happened in my Order Form, it would associate the order lines with the wrong order! So that is why I am going out of my way to program this functionality.
 
Back
Top