sara sara - 24 days ago 9
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 p.id.ToString() == 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 p.id.ToString() == 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

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

 try
        {
            context.SubmitChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            // Provide for exceptions.
        }

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

this.Refresh();
Comments