none
Problem updating db table using Linq RRS feed

  • Question

  • Hi Everyone,
     I'm hoping I picked the right forum for my question. If not, apologize in advance. 

     My question is about accesing and modifying a database using Linq. I have the following test code, running agains a Table named Currency whcih has two fields, string Symbol and float ConversionFactor. When I run this program, I see that my table is not modified. Code and Output listed below.

     I'm trying to figure out what I'm doing wrong. Any help is greatly appreciated. 

     I'm using Visual Studio 2008 SP 1. Language: C#

    Thanks much.

    Joe 

    --------- code ----------------

    DataClasses1DataContext
    db = new DataClasses1DataContext();

    Currency cur = db.Currencies.Single(p => p.Symbol == "JPY");

    Console.WriteLine("Initial ConversionRate for {0} is {1}", cur.Symbol, cur.ConversionFactor);

    float f = (float)cur.ConversionFactor;

    cur.ConversionFactor = f/2;

    Console.WriteLine("Changing ConversionRate for {0} to {1}", cur.Symbol, cur.ConversionFactor);

    db.SubmitChanges();

    Currency cur2 = db.Currencies.Single(p => p.Symbol == "JPY");

    Console.WriteLine("Modified ConversionRate for {0} is {1}", cur2.Symbol, cur2.ConversionFactor);

    -----------output ------------------

    Initial ConversionRate for JPY  is 101.61

    Changing ConversionRate for JPY to 50.805

    Modified ConversionRate for JPY is 101.61.

    Thursday, January 15, 2009 5:04 PM

All replies

  • HI

    Do you have primary key for that table? if no please add the primary key column to the table and update the .dbml file then try it. i hope this will help you

    thanks

     

    Tuesday, August 16, 2011 10:08 AM
  • I might be off in the wrong direction here, but shouldn't the line where you retrieve cur2 return exactly the same object as cur 1 (i.e. a ReferenceEquals) because they are being read from the same DataClasses1DataContext object?

    Could you check the value in 'cur' after the SubmitChanges() please?

    The DataClasses1DataContext object caches objects which have already been read from the database so it shouldn't go to the database again if it already has it.

     

     

    Wednesday, August 17, 2011 1:58 PM
  • I've just noticed the date of the original posting - methinks that we won't get a reply from the OP.
    Thursday, August 18, 2011 1:14 PM