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 with Linq?

Comparing rows with Linq?

Scheduled Pinned Locked Moved LINQ
databasecsharplinqquestionannouncement
5 Posts 2 Posters 4 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 lets say I've got a table in a SQL data base with the fields ID, FirstName, LastName now in my application the user can edit a row in the db. The contents of this table is displayed on a DataGridView and once a row is double clicked another form opens with that particular rows data filled in. Once the data has been edited the user clicks "SAVE" and a stored procedure is called to update the data in the db. now what i need is that after the data has been updated i must know what changed and what are the old and new values. At the moment it works something similar to this

    public partial class Form1 : Form
    {
    int tempId;
    string tempFirstName;
    string tempLastName;

        public Form1()
        {
            InitializeComponent();
    
            //
            // Nothing happens here, this constructor is used 
            // when the user wishes to add a new row to the db
            //
        }
    
        public Form1(int ID)
        {
            InitializeComponent();
    
            //
            // This constuctor is used when the user wishes to
            // edit a row in the db, so the old values need to
            // be queried and filled into the controles on the form
            //
    
            //
            // create and Open the sql connection
            string connString = ConfigurationManager.ConnectionStrings\["sqlConnString"\].ConnectionString;
            SqlConnection mySqlConn = new SqlConnection(connString);
            mySqlConn.Open();
    
            //
            // Initialize the stored procedure
            SqlCommand insertCommand = new SqlCommand("Proc\_SelectPersontByID", mySqlConn);
            insertCommand.CommandType = CommandType.StoredProcedure;
    
            insertCommand.Parameters.AddWithValue("@ID", ID);
    
            //
            // Execute the reader
            SqlDataReader reader = insertCommand.ExecuteReader();
            reader.Read();
    
            //
            // Fill in the initial values
            txtID.Text = reader\["ID"\].ToString();
            txtFirstName.Text = reader\["FirstName"\].ToString();
            txtLastName.Text = reader\["LastName"\].ToString();
    
            //
            // Now we need to save these values for comparison later
            tempId = reader\["ID"\].ToString();
            tempFirstName = reader\["FirstName"\].ToString();
            tempLastName = reader\["LastName"\].ToString();
        }
    
        private void btnSave\_Click(object s
    
    P 1 Reply Last reply
    0
    • H Harvey Saayman

      Hey guys lets say I've got a table in a SQL data base with the fields ID, FirstName, LastName now in my application the user can edit a row in the db. The contents of this table is displayed on a DataGridView and once a row is double clicked another form opens with that particular rows data filled in. Once the data has been edited the user clicks "SAVE" and a stored procedure is called to update the data in the db. now what i need is that after the data has been updated i must know what changed and what are the old and new values. At the moment it works something similar to this

      public partial class Form1 : Form
      {
      int tempId;
      string tempFirstName;
      string tempLastName;

          public Form1()
          {
              InitializeComponent();
      
              //
              // Nothing happens here, this constructor is used 
              // when the user wishes to add a new row to the db
              //
          }
      
          public Form1(int ID)
          {
              InitializeComponent();
      
              //
              // This constuctor is used when the user wishes to
              // edit a row in the db, so the old values need to
              // be queried and filled into the controles on the form
              //
      
              //
              // create and Open the sql connection
              string connString = ConfigurationManager.ConnectionStrings\["sqlConnString"\].ConnectionString;
              SqlConnection mySqlConn = new SqlConnection(connString);
              mySqlConn.Open();
      
              //
              // Initialize the stored procedure
              SqlCommand insertCommand = new SqlCommand("Proc\_SelectPersontByID", mySqlConn);
              insertCommand.CommandType = CommandType.StoredProcedure;
      
              insertCommand.Parameters.AddWithValue("@ID", ID);
      
              //
              // Execute the reader
              SqlDataReader reader = insertCommand.ExecuteReader();
              reader.Read();
      
              //
              // Fill in the initial values
              txtID.Text = reader\["ID"\].ToString();
              txtFirstName.Text = reader\["FirstName"\].ToString();
              txtLastName.Text = reader\["LastName"\].ToString();
      
              //
              // Now we need to save these values for comparison later
              tempId = reader\["ID"\].ToString();
              tempFirstName = reader\["FirstName"\].ToString();
              tempLastName = reader\["LastName"\].ToString();
          }
      
          private void btnSave\_Click(object s
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The DataContext supplies a GetChangeSet method that should help.

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

      My blog | My articles

      H 1 Reply Last reply
      0
      • P Pete OHanlon

        The DataContext supplies a GetChangeSet method that should help.

        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 ill have a look at it tomorrow morning, i only learnt Linq today :rolleyes:

        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 ill have a look at it tomorrow morning, i only learnt Linq today :rolleyes:

          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

          HarveySaayman wrote:

          i only learnt Linq today

          Wow, that's quick. I've only been learning it for a year now.

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

          My blog | My articles

          H 1 Reply Last reply
          0
          • P Pete OHanlon

            HarveySaayman wrote:

            i only learnt Linq today

            Wow, that's quick. I've only been learning it for a year now.

            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

            im no expert... yet :) But i get the basic concept. played around with Linq to objects today and read through Linq to SQL and Linq to XML in "Pro C# 2008 and .Net 3.5 Platform". gona do some practical Linq to SQL tomorow morning before i start implementing it in my project :)

            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