I work with several code pages and Stored Procedures during day. I look for an easy way to stop visual studio while debugging, so database changes rollback. Since I work with several pages, i don't want to add code for this in my pages or for every SP I use. Is there a shortcut or a command i can use in immediate window?(I am open to other solutions - these are the ones came into my mind) If I can manually throw an exception(not written in code, in immediate window may be, it may work.)
-
Please provide a code sample of how you're currently accessing the database in code.Steve Lillis– Steve Lillis2015-06-11 08:48:02 +00:00Commented Jun 11, 2015 at 8:48
-
A very easy way to see if manually throwing an exception in the Immediate window will skip the database calls is to try throwing an exception in the immediate window. Did you try this? What happened?amcdermott– amcdermott2015-06-11 09:13:12 +00:00Commented Jun 11, 2015 at 9:13
-
Thanks for the answers. I tried using immediate window. It says "Invalid expression term 'throw'". I am using company framework, ı could not look into sql connection - it is too complicatedYalçın Aktaş– Yalçın Aktaş2015-06-11 10:56:07 +00:00Commented Jun 11, 2015 at 10:56
-
Look at my answer, if you want to throw an exception from immediate window.Rafał Straszewski– Rafał Straszewski2015-06-11 12:00:53 +00:00Commented Jun 11, 2015 at 12:00
-
ı replied below, thanks anyway - since we try together.Yalçın Aktaş– Yalçın Aktaş2015-06-11 12:04:26 +00:00Commented Jun 11, 2015 at 12:04
Add a comment
|
3 Answers
I think the best thing is to use TransactionScope class for this.
Just wrap your db calls inside
using (TransactionScope scope = new TransactionScope())
{
// Invoke your database calls
scope.Complete(); // Put a break point here and stop the program when the break point is hit.
}
1 Comment
Yalçın Aktaş
Not possible, a lot of people work on these code, i can not change it except it is necessary.
If you want to throw an exception, you can do it using Immediate Window, after hitting breakpoint.
The problem is, you can not use in Immediate Window something like:
throw new ApplicationException("Forced exception");
But you can create the method, and then launch it from Immediate Window
public static void ForceAnException()
{
throw new ApplicationException("Forced exception");
}
In immediate window:
ForceAnException();
EDIT:
So maybe try this:
var throwEx = false;
if(throwEx) ForceAnException();
And change in immediate window throwEx to true, before condition is checked...it should work...
6 Comments
Lasse V. Karlsen
If he's already at a breakpoint, inside a transaction, wouldn't simply killing the program do the trick?
Rafał Straszewski
I think, he is at a breakpoint in try block, and he want to do something, to jump to catch, where is database rollback command. Killing application before rollback will not do the trick, because some data could be already commited to database.
Lasse V. Karlsen
Then he should be able to just drag the execution point, shouldn't he? And if he's inside a transaction, nothing is really committed yet, so if he closes the connection (which will happen if he kills the application) then everything is rolled back automatically.
Rafał Straszewski
Hmm, I don't think, that moving execution point between blocks is possible (from try to catch) And yes, in theory nothing should be commited before closing transaction :) I am not sure how op's code is looking, so I can not really tell..
Yalçın Aktaş
Thanks for the answers, Rafal it is just like you said. But when i do as you said, immediate window function call (throwing exception) didn't go to Exception Handler, it worked but didn't do anything, keep going from where it left. But when i wrote the same function in code-it is called by VS, it works exactly as i want. What may be the problem?
|