sara sara - 1 year ago 67
C# Question

Update one field of specific records using linq

I have 2 TextBoxes for enter 2 id and a TextBox for enter amount.I want to add this amount to amount in my table with first id,and minus to amount in my table with second id.
This is my code:

int money = Int16.Parse(textBox3.Text);

List<account> q_to = (from p in context.accounts where == textBox2.Text select p).ToList();
foreach (account p in q_to)
p.amount = p.amount + money;

List<account> q_from = (from p in context.accounts where == textBox1.Text select p).ToList();
foreach (account p in q_from)
if (p.amount < money)
MessageBox.Show("Account balance is not enough");
p.amount = p.amount - money;

But just update first row.
What should i do?

Answer Source

Should create this code if you want to save changes in database:

        catch (Exception ex)
            // Provide for exceptions.

And if you want to see updates on your database,you should refresh the form :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download