5

So, here is my hopefully unique spin on this common problem.

I do my query, get my objects then pass the object into a form where it populates the form with the data from the object (this is not passed in by reference).

I then edit the values of the object that was queried (via the form) and then return a new object constructed from the values in the form.

I then want to update this to the database. Attach does nothing (runs but does not update). SubmitChanges also does nothing (and both do nothing when used together).

What am I missing?

Update: here is the code I am using:

// In constructor
_dataMap = new DataMapDataContext();
_addresses = _dataMap.AddressItems
         .Where(address => address.InsertUserName == _currentUser.Name).ToList();



public void EditButtonClick()
{
    using (AddAddressForm form = new AddAddressForm(_addresses[_currentAddress]))
    {
        form.Text = "Edit Address";
        if (DialogResult.OK == form.ShowDialog())
        {
            _addresses[_currentAddress] = form.Item;
            _dataMap.SubmitChanges();
            DisplayItem();
        }
    }
}
4
  • 4
    Dude, you asked this question yesterday, which i answered (and was ticked correct) - stackoverflow.com/questions/3756890/… whats the issue? Commented Sep 22, 2010 at 4:58
  • 2
    Also, post the code you've tried, which isnt working - otherwise people will "guess" what you have/havent done - the below answer is an example of that Commented Sep 22, 2010 at 5:00
  • @RPM1984 - true, they are very similar. But this one is an update that one was inserts. (I don't think InsertOnSumbit will work for updated items). Commented Sep 22, 2010 at 7:04
  • 1
    so let me get this right, youre newing up the DataContext in the constructor, setting it to a public instance variable, then trying to add some changes to it on the click event? doesnt really make sense. where are you disposing of the context? and what is AddAddressForm (and why is that in a using). @Naaeem's answer is basically correct (except the code should be wrapped in using) Commented Sep 22, 2010 at 7:13

2 Answers 2

7

You'll need to get the record from the database, update it's values and then call SubmitChanges()

using(MyDataContext db = new MyDataContext())
{
    // get the record
    Product dbProduct = db.Products.Single(p => p.ID == 1);

    // set new values
    dbProduct.Quantity = 5; 
    dbProduct.IsAvailable = false;

    // save them back to the database
    db.SubmitChanges();
}
Sign up to request clarification or add additional context in comments.

2 Comments

where's your using/dispose? tsk, tsk. =)
N.B. while this is the clean way to do it, it should be noted that if you create a new query in LinqPad, you can also omit the data context and type into the Main method: var dbProduct = Products.Single(p => p.ID == 1); dbProduct.Quantity = 5; SubmitChanges(); - for quick update queries useful. Note: This only works if you have selected a database in the LinqPad query dropdown beforehand.
-4

Turns out I was doing almost everything right.

I just needed to pass in the object I was editing by reference. That way when it got changed, it was not a new object that was returned, but the same one (that Linq-to-SQL already knew about.)

These are the two lines from the code above that got changed:

AddressItem itemToEdit = _addresses[_currentAddress];
using (AddAddressForm form = new AddAddressForm(ref itemToEdit))

3 Comments

@msarchet - This is just for a demo, so it is not that important now, but for future reference, why is this wrong? I am not a huge user of Linq-to-SQL and when I do use it I usually have it calling stored procedures for my inserting and updating. So I am not familiar with this aspect of Linq-To-SQL. If you have a better way that has a different class than the one that has the data context and the query doing the updating then post it as an answer and I will select it. If not, well then....
So many downvotes and no explanation as to why this is bad. Got to love cowardly downvoters.
I can't see how that answers the question "How to save changes in Linq-to-SQL?" - SubmitChanges() is the right method to call. But you need to query first and then make the changes on the queried object(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.