Link to home
Start Free TrialLog in
Avatar of Samiha Kaiser
Samiha Kaiser

asked on

Help doing Entity Framework C#

I am doing Entity Framework and I have a database where there is no connection between the two tables. My form has two datagridviews each showing the contents of those tables on load. I also have two buttons. When  selected any cell (which selects the entire row)  on  the sport table and click copy it moves the data to the health table and removes it from the sport. The second button does vise versa. what would be like? User generated imageUser generated image
Avatar of kaufmed
kaufmed
Flag of United States of America image

Your question is unclear. Can you rephrase?
Avatar of Samiha Kaiser
Samiha Kaiser

ASKER

the two buttons, they will be “twin methods” each one doing almost exactly the same thing.  The button I called “Move from Sporting to Health” will take the object in the entity collection corresponding to the row selected in the Sporting Goods datagridview, delete it from that collection (and preserve the change to SQL ), and add a new corresponding object into the HealthItem collection (and preserve the change to SQL), and then update the display to show the item moved from one table to the other.  The button I labeled “Move from Health to Sporting” does a similar thing, moving an item out of the HealthItems table and into the SportingItems table.
I hope this is more clearer
Capture3.PNG
Are you using data binding to fill these rows in the DataGridViews, or are you filling them manually (e.g. by looping over the records retrieved from the database)?
This is my code so far in the attached
Capture5.PNG
My updated code
Capture5.PNG
In your MoveToHealth_Click function, you're creating a new HealthItem(), but you're never populating it, so you're effectively adding a blank record.

var data2 = new HealthItem();
....
// now populate data2
data2.Name = rowToAdd.Name;
data2.Description = rowToAdd.Description;
data2.QuantityOnHand = rowToAdd.QuantityOnHand;

mySports.HealthItems.Add(data2);

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.