How to know which exception to be thrown?

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
 
M

Munsifali Rashid

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top