How to know which exception to be thrown?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

Hi

I have a method which insert data to DB. The data can be
inserted. However, it returns to the login.aspx everytime.
There is an exception to be thrown. But, I do not know
what is the exception? How can I show it? The following is
my code.

try
{
objOperator.Insert(this.UserName.Text,this.Password.Text);
}
catch(Exception)
{
Response.Redirect("/login.aspx",true);
}
Response.Redirect("/index.aspx",true);

Thanks
 
You need to do something with the exception after you catch it:

Eg.

try
{
// Verify user details
objOperator.Insert(this.UserName.Text,this.Password.Text);

// Redirect to the index page
Response.Redirect("index.aspx");
}
catch (Exception ex)
{
// This will write the error message to the debugger window
System.Diagnostics.Debugger.WriteLine("Error logging in: " +
ex.Message);

// Alternatively, you can pass the error back to the login page
Response.Redirect("login.aspx?error=" +
HttpUtility.UrlEncode(ex.Message));
}

However, as this code will probably execute from the login page itself, you
don't need to redirect back to the same page. You could just have a label
on the page, which is iniatially set to something like "Please Log In", and
then on the login button click event, attempt to log the user in and
redirect them if it's successful. Otherwise, update the label with the
error message.

Regards,

Mun
 
Back
Top