Where to place the redirection statement

Using the this article - http://www.codeproject.com/KB/database/transactions.aspx, where do I place my redirection statement?

Say for example, after a successful insertion of a new record I am going to redirect the page to success.aspx saying “A new record has been successfully added”.

Where am I going to place my redirection statement?

Is it in this way


//create and open connection
//begin transaction
try
{
  //create query
  //execute query
 //commit
 Response.Redirect("success.aspx");
}
catch
{
     //rollback
}
finally
{
    //close connection
}

Or in this way?


//create and open connection
//begin transaction
try
{
  //create query
  //execute query
 //commit
}
catch
{
     //rollback
}
finally
{
    //close connection
}
Response.Redirect("success.aspx");

I am bit confuse with it. Please give me an idea.

No, you should always have try,catch,finaly blocks. As if something goes wrong, the connection will not be closed unless it in the finaly block. If all goes well, it will get closed in the try before the redirect. And if no redirect, the finaly should check the status before it tries to close it.

But the above examples will also work

you need to close the connection for better db performance.

try this:

//create and open connection
//begin transaction
bool success=false;
try
{
//create query
//execute query
//commit
success=true;
}
catch
{
success=false;
//rollback
}
finally
{
//close connection
}
if(success)
Response.Redirect(“success.aspx”);

Then, therefore, there is no need for me to make use of the finally block, is it?

No, you will have to close it before you redirect

That should do

If I’ll be using the first option, does the connection to the database also closes?

The first option is what you want to use. As the last option will redirect even if something fails, as it is after the try catch blocks