Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Comparing Rows .... AGAIN

Comparing Rows .... AGAIN

Scheduled Pinned Locked Moved LINQ
databasesalesquestionannouncementworkspace
8 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harvey Saayman
    wrote on last edited by
    #1

    Hey guys please consider the following method that updates a row in a db

    private static void editClient(DataClasses1DataContext ctx)
    {
    Console.WriteLine("<---<<< Updating A Client>>>--->");
    Console.ReadLine();
    Console.Clear();

    Console.Write("Please Enter Customer ID --> ");
    int ID = Int32.Parse(Console.ReadLine());
    Console.Clear();
    
    //
    // Save The Row Before changing it
    //
    var myOldRow = (from client in ctx.Clients
                    where client.clientID == ID
                    select client).First();
    
    //
    // Get the other field values from the user
    //
    Console.Write("Please Enter Customer First Name --> ");
    string firstName = Console.ReadLine();
    Console.Clear();
    
    Console.Write("Please Enter Customer Last Name --> ");
    string lastName = Console.ReadLine();
    Console.Clear();
    
    Console.Write("Please Enter Customer Tellephone Number --> ");
    string phoneNum = Console.ReadLine();
    Console.Clear();
    
    var myNewRow = (from client in ctx.Clients
                    where client.clientID == ID
                    select client).First();
    
    
    myNewRow.clientFirstName = firstName;
    myNewRow.clientLastName = lastName;
    myNewRow.clientPhoneNumber = phoneNum;
    
    ctx.SubmitChanges();
    
    //
    // Compare The Values Here
    // 
    
    string updateDetails = string.Empty;
    
    if (myOldRow.clientFirstName != myNewRow.clientFirstName)
    {
        updateDetails += "Changed First Name From " + myOldRow.clientFirstName + " To " + myNewRow.clientFirstName + Environment.NewLine;
    }
    
    if (myOldRow.clientLastName != myNewRow.clientLastName)
    {
        updateDetails += "Changed Last Name From " + myOldRow.clientLastName + " To " + myNewRow.clientLastName + Environment.NewLine;
    }
    
    if (myOldRow.clientPhoneNumber != myNewRow.clientPhoneNumber)
    {
        updateDetails += "Changed Phone Number From " + myOldRow.clientPhoneNumber + " To " + myNewRow.clientPhoneNumber + Environment.NewLine;
    }
    
    Console.Clear();
    Console.WriteLine("<---<<< Change Log >>>--->\\r\\n\\r\\n");
    Console.WriteLine(updateDetails);
    Console.Read();
    

    }

    now this isnt working, after the update myOldRow is updated and has the new values in it. so my if statements never evaluate to a true. is there a way that i can disconnect myOldRow so that it does not get updated?

    P 1 Reply Last reply
    0
    • H Harvey Saayman

      Hey guys please consider the following method that updates a row in a db

      private static void editClient(DataClasses1DataContext ctx)
      {
      Console.WriteLine("<---<<< Updating A Client>>>--->");
      Console.ReadLine();
      Console.Clear();

      Console.Write("Please Enter Customer ID --> ");
      int ID = Int32.Parse(Console.ReadLine());
      Console.Clear();
      
      //
      // Save The Row Before changing it
      //
      var myOldRow = (from client in ctx.Clients
                      where client.clientID == ID
                      select client).First();
      
      //
      // Get the other field values from the user
      //
      Console.Write("Please Enter Customer First Name --> ");
      string firstName = Console.ReadLine();
      Console.Clear();
      
      Console.Write("Please Enter Customer Last Name --> ");
      string lastName = Console.ReadLine();
      Console.Clear();
      
      Console.Write("Please Enter Customer Tellephone Number --> ");
      string phoneNum = Console.ReadLine();
      Console.Clear();
      
      var myNewRow = (from client in ctx.Clients
                      where client.clientID == ID
                      select client).First();
      
      
      myNewRow.clientFirstName = firstName;
      myNewRow.clientLastName = lastName;
      myNewRow.clientPhoneNumber = phoneNum;
      
      ctx.SubmitChanges();
      
      //
      // Compare The Values Here
      // 
      
      string updateDetails = string.Empty;
      
      if (myOldRow.clientFirstName != myNewRow.clientFirstName)
      {
          updateDetails += "Changed First Name From " + myOldRow.clientFirstName + " To " + myNewRow.clientFirstName + Environment.NewLine;
      }
      
      if (myOldRow.clientLastName != myNewRow.clientLastName)
      {
          updateDetails += "Changed Last Name From " + myOldRow.clientLastName + " To " + myNewRow.clientLastName + Environment.NewLine;
      }
      
      if (myOldRow.clientPhoneNumber != myNewRow.clientPhoneNumber)
      {
          updateDetails += "Changed Phone Number From " + myOldRow.clientPhoneNumber + " To " + myNewRow.clientPhoneNumber + Environment.NewLine;
      }
      
      Console.Clear();
      Console.WriteLine("<---<<< Change Log >>>--->\\r\\n\\r\\n");
      Console.WriteLine(updateDetails);
      Console.Read();
      

      }

      now this isnt working, after the update myOldRow is updated and has the new values in it. so my if statements never evaluate to a true. is there a way that i can disconnect myOldRow so that it does not get updated?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      There is a Detach method that you might want to look at.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      H 1 Reply Last reply
      0
      • P Pete OHanlon

        There is a Detach method that you might want to look at.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        H Offline
        H Offline
        Harvey Saayman
        wrote on last edited by
        #3

        Thanx pete :) Which object has this Detach() method? i dont see it in my myOldRow or ctx Objects? OT: While waiting for your reply (i knew you were going too cuz it seems not alot of CPians seem to use Linq or they aren't comfortable enough to answer questions on the subject) i found your series on going solo. Im busy reading and loving it. Ill let you know what i think when i get to the last published one in the series. Thanx again for all your help

        Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

        you.suck = (you.passion != Programming)

        P 1 Reply Last reply
        0
        • H Harvey Saayman

          Thanx pete :) Which object has this Detach() method? i dont see it in my myOldRow or ctx Objects? OT: While waiting for your reply (i knew you were going too cuz it seems not alot of CPians seem to use Linq or they aren't comfortable enough to answer questions on the subject) i found your series on going solo. Im busy reading and loving it. Ill let you know what i think when i get to the last published one in the series. Thanx again for all your help

          Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

          you.suck = (you.passion != Programming)

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Doh. :doh: Of course you haven't got Detach - it's part of our Linq business layer. Anyway, the solution to your problem is to create a new DataContext instance just before you retrieve myNewRow. I'm glad you like the series - there are more coming, but I'm really tied up with some of the clients right now and I haven't had the chance to come out from under. I'll shortly be releasing a Linq business layer to "the masses" that should help to remove a lot of the burdens that are currently there. I'm in the process of sanitizing it and adding a new validation layer to it.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          H 2 Replies Last reply
          0
          • P Pete OHanlon

            Doh. :doh: Of course you haven't got Detach - it's part of our Linq business layer. Anyway, the solution to your problem is to create a new DataContext instance just before you retrieve myNewRow. I'm glad you like the series - there are more coming, but I'm really tied up with some of the clients right now and I haven't had the chance to come out from under. I'll shortly be releasing a Linq business layer to "the masses" that should help to remove a lot of the burdens that are currently there. I'm in the process of sanitizing it and adding a new validation layer to it.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            H Offline
            H Offline
            Harvey Saayman
            wrote on last edited by
            #5

            cool looking forward to it :) thanx for the help, ill let you know if it works or not.

            Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

            you.suck = (you.passion != Programming)

            1 Reply Last reply
            0
            • P Pete OHanlon

              Doh. :doh: Of course you haven't got Detach - it's part of our Linq business layer. Anyway, the solution to your problem is to create a new DataContext instance just before you retrieve myNewRow. I'm glad you like the series - there are more coming, but I'm really tied up with some of the clients right now and I haven't had the chance to come out from under. I'll shortly be releasing a Linq business layer to "the masses" that should help to remove a lot of the burdens that are currently there. I'm in the process of sanitizing it and adding a new validation layer to it.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              H Offline
              H Offline
              Harvey Saayman
              wrote on last edited by
              #6

              Thanx pete, your solution worked :) you have NO IDEA how much work this is gona save me... I have some more questions on Linq if you dont mind... 1) You mentioned in another thread that using a single *.dbml file has problems, what happens? 2) At which point of the DataContext class open and close the sqlConnection? if its at creation and disposal then ill have to create, do what needs to be done and dispose it... correct? 3) Is there anything else that i should look out for? There was more questions but i can remember them now :rolleyes: TTYL

              Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

              you.suck = (you.passion != Programming)

              P 1 Reply Last reply
              0
              • H Harvey Saayman

                Thanx pete, your solution worked :) you have NO IDEA how much work this is gona save me... I have some more questions on Linq if you dont mind... 1) You mentioned in another thread that using a single *.dbml file has problems, what happens? 2) At which point of the DataContext class open and close the sqlConnection? if its at creation and disposal then ill have to create, do what needs to be done and dispose it... correct? 3) Is there anything else that i should look out for? There was more questions but i can remember them now :rolleyes: TTYL

                Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

                you.suck = (you.passion != Programming)

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Harvey

                HarveySaayman wrote:

                1. You mentioned in another thread that using a single *.dbml file has problems, what happens?

                See this[^] blog entry for the reason why. It really caught me out at first.

                HarveySaayman wrote:

                At which point of the DataContext class open and close the sqlConnection? if its at creation and disposal then ill have to create, do what needs to be done and dispose it... correct?

                The important thing to remember about L4S is that the load operation isn't necessarily when you think it is. Just because you have created the query, this doesn't mean you have loaded the data. This is done for you at the point at which you perform one of the fill methods (such as First()). Linq takes care of the open and close for you so that you don't need to worry about it.

                HarveySaayman wrote:

                1. Is there anything else that i should look out for?

                Not so much a thing to watch out for, but more something that is really really cool. Take a look at my blog post on refining queries[^]. A big gotcha is that default entries on your database aren't respected by Linq (and this is a real shortcoming as far as I'm concerned). For instance, if you have a datetime field that should use GetDate() to populate it, you're stuffed - GETDATE() doesn't get called automatically. See my blog entry here[^].

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                H 1 Reply Last reply
                0
                • P Pete OHanlon

                  Harvey

                  HarveySaayman wrote:

                  1. You mentioned in another thread that using a single *.dbml file has problems, what happens?

                  See this[^] blog entry for the reason why. It really caught me out at first.

                  HarveySaayman wrote:

                  At which point of the DataContext class open and close the sqlConnection? if its at creation and disposal then ill have to create, do what needs to be done and dispose it... correct?

                  The important thing to remember about L4S is that the load operation isn't necessarily when you think it is. Just because you have created the query, this doesn't mean you have loaded the data. This is done for you at the point at which you perform one of the fill methods (such as First()). Linq takes care of the open and close for you so that you don't need to worry about it.

                  HarveySaayman wrote:

                  1. Is there anything else that i should look out for?

                  Not so much a thing to watch out for, but more something that is really really cool. Take a look at my blog post on refining queries[^]. A big gotcha is that default entries on your database aren't respected by Linq (and this is a real shortcoming as far as I'm concerned). For instance, if you have a datetime field that should use GetDate() to populate it, you're stuffed - GETDATE() doesn't get called automatically. See my blog entry here[^].

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  H Offline
                  H Offline
                  Harvey Saayman
                  wrote on last edited by
                  #8

                  Thanx pete i think im ready to start implementing sum Linq stuff into my project now. thanx for all your help bud :)

                  Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

                  you.suck = (you.passion != Programming)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups