1

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.)

5
  • Please provide a code sample of how you're currently accessing the database in code. Commented 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? Commented 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 complicated Commented Jun 11, 2015 at 10:56
  • Look at my answer, if you want to throw an exception from immediate window. Commented Jun 11, 2015 at 12:00
  • ı replied below, thanks anyway - since we try together. Commented Jun 11, 2015 at 12:04

3 Answers 3

1

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.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not possible, a lot of people work on these code, i can not change it except it is necessary.
0

I usually kill the w3wp.exe process using Process Explorer. Alternatively, you can drag and drop the "yellow mark" in the debugger to a point after the commit so that the commit is skipped. I also sometimes set a variable to null and induce a crash that way.

Comments

0

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

If he's already at a breakpoint, inside a transaction, wouldn't simply killing the program do the trick?
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.
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.
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..
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.