How to add new (empty) types rows to TableAdapter with contraints?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm struggling with relationship constraints, when adding new rows. If I want
to add a single row in a single-table scenario and let the user enter values
on a fom, it's easy. I just call the TA.Add{tablename}Row() method. In the
edit form I bind and do an Update() when done.

Problem? Say I have a parent record that I want the user to select from a
drop-down? The Add{tablename}Row() method fails because my DB constraints
don't allow for a NULL FK to the parent table.

That makes sense, but how do I get a place hold, let the user fill it in
(select parent) and on the Update() fail it if they didn't select a parent?
 
Well, this is trully a problem. i guess your choices are these:
- when you create new row, fill not null fields with default values
- set column as null allowed and make sure that value exists before doing
update
 
This is really trivial.

1) On your form, add two datasets. Lets call the first one dsCustomer and
the second one dsSalesRep where each customer is assigned a sales rep.

2) Add two binding sources. The first one binds to customer (bsCustomer) and
the second to salesrep (bsSalesRep).

3) Add the fields for a customer - bind this to bsCustomer.

4) Add a combo box to the form. Set its datasource to bsSalesRep, its
displaymember to SalesmanName and its valuemember to SalesRepID assuming
the SalesRepID is the foreign key. Bind the combo box itself to
bsCustomer.SalesRepID - a field in the customer table that references the
foreign key.

5) In your code, have a line like bsCustomer.AddNew() or use a data
navigator bound to bsCustomer to do it for you. Do not even think of doing
anything with table adapter at this point. Think binding sources - that is
what they are for. You use the table adapter to update the database once
the entry is done.


Miha Markic said:
Well, this is trully a problem. i guess your choices are these:
- when you create new row, fill not null fields with default values
- set column as null allowed and make sure that value exists before doing
update

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Todd Beaulieu said:
I'm struggling with relationship constraints, when adding new rows. If I
want
to add a single row in a single-table scenario and let the user enter
values
on a fom, it's easy. I just call the TA.Add{tablename}Row() method. In
the
edit form I bind and do an Update() when done.

Problem? Say I have a parent record that I want the user to select from a
drop-down? The Add{tablename}Row() method fails because my DB constraints
don't allow for a NULL FK to the parent table.

That makes sense, but how do I get a place hold, let the user fill it in
(select parent) and on the Update() fail it if they didn't select a
parent?
 
Back
Top