Comparing rows with Linq?
-
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
-
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
The DataContext supplies a GetChangeSet method that should help.
Deja View - the feeling that you've seen this post before.
-
The DataContext supplies a GetChangeSet method that should help.
Deja View - the feeling that you've seen this post before.
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)
-
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)
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.
-
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.
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)