0

This is my code:

... Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text); updatedBox = getBoxInfo(); entities.SaveChanges(); private Domain.Box getBoxInfo() { Domain.Box retBox = new Domain.Box(); retBox.BoxID = TextBoxBoxID.Text; retBox.LocationID = Convert.ToDecimal(TextBoxLocationID.Text); retBox.Positions = Convert.ToByte(TextBoxPositions.Text); retBox.DiseaseID = RadComboBoxDisease.SelectedValue; retBox.SampleTypeID = RadComboBoxSampleType.SelectedValue; retBox.TubeTypeId = RadComboBoxTubeTypeID.SelectedValue; return retBox; } 

The code compiles and executes fine, but the database does not changes, this is, all the information is exactly the same as it was before the update. Any help will be appreciated, thanks!

2
  • I don't understand what you're trying to do here. Retrieve a Domain.Box object and then set its value to a new object? Commented Jun 29, 2011 at 19:43
  • 1
    webyac, you are overwriting a reference. Nothing of the FirstOrDefault box remains. Commented Jun 29, 2011 at 19:50

3 Answers 3

2

If you want to insert a new Domain.Box object, you should do it like this:

entities.Boxes.AddObject(getBoxInfo()); entities.SaveChanges(); 

There is no need to create the updatedBox object because you're just overwriting it. If I understand you're requirements, you want to perform an insert, not an update.

If I'm incorrect and you're trying to update certain properties of the updatedBox object, then just pass a reference to the object and update it's properties:

Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text); getBoxInfo(ref updatedBox); entities.SaveChanges(); private void getBoxInfo(ref Domain.Box retBox) { retBox.BoxID = TextBoxBoxID.Text; ... } entities.SaveChanges(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was missing indeed the reference to the ET object
0

I think you want to do this:

Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text); UpdateBoxInfo(updatedBox); entities.SaveChanges(); private void UpdateBoxInfo(Domain.Box theBox) { theBox.BoxID = TextBoxBoxID.Text; theBox.LocationID = Convert.ToDecimal(TextBoxLocationID.Text); theBox.Positions = Convert.ToByte(TextBoxPositions.Text); theBox.DiseaseID = RadComboBoxDisease.SelectedValue; theBox.SampleTypeID = RadComboBoxSampleType.SelectedValue; theBox.TubeTypeId = RadComboBoxTubeTypeID.SelectedValue; } 

Comments

0

I'd try it this way:

Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text); getBoxInfo(updatedBox); entities.SaveChanges(); private void getBoxInfo(Domain.Box retBox) { retBox.LocationID = Convert.ToDecimal(TextBoxLocationID.Text); retBox.Positions = Convert.ToByte(TextBoxPositions.Text); retBox.DiseaseID = RadComboBoxDisease.SelectedValue; retBox.SampleTypeID = RadComboBoxSampleType.SelectedValue; retBox.TubeTypeId = RadComboBoxTubeTypeID.SelectedValue; } 

2 Comments

I don't think you need to pass by reference unless Domain.Box is a value type.
And BoxId probably should not be changed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.