Comparing Rows .... AGAIN
-
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 disconnectmyOldRow
so that it does not get updated? -
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 disconnectmyOldRow
so that it does not get updated?There is a
Detach
method that you might want to look at.Deja View - the feeling that you've seen this post before.
-
There is a
Detach
method that you might want to look at.Deja View - the feeling that you've seen this post before.
Thanx pete :) Which object has this
Detach()
method? i dont see it in mymyOldRow
orctx
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 helpHarvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
-
Thanx pete :) Which object has this
Detach()
method? i dont see it in mymyOldRow
orctx
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 helpHarvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
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.
-
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.
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)
-
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.
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)
-
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)
Harvey
HarveySaayman wrote:
- 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:
- 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.
-
Harvey
HarveySaayman wrote:
- 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:
- 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.
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)