Click here to Skip to main content
15,921,250 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hellow.i have a class Called Students where i have several methods.In my method for creating students,i first check if that student exists then i throw error back to him if found.What it brings back to me is the Exception since i have used a throw statement and it brings that annoying YELLOW SCREEN OF DEATH .I need someone to tell me how i can use a response.redirect so as instead of throwing that error,it redirects him to a custom error page i have created,

Here is the code on the Students Class

C#
Student exist = db.Students.FirstOrDefault(x => x.studentName == studentName);
           if (exist != null)
           {


               throw new Exception("Student Exists");
           }

Any how i can use a redirect to my error page or how i can call it on my webform code behind to use the line their?Thanks
Posted
Updated 22-Jan-14 2:40am
v2
Comments
Karthik_Mahalingam 22-Jan-14 9:03am    
Student is a webpage or separate class ?
Member 10398773 22-Jan-14 9:13am    
It's a separate class
Karthik_Mahalingam 22-Jan-14 9:17am    
ok

Hello,
You have to use a Global.asax file, and implement the Application_Error method :
MSDN : How to: Handle Application-Level Errors[^]
 
Share this answer
 
For this you will have to write error handling code in Application_Error event handler in the Global.asax file. It is called when any unhandle exception occurs.

Code as follows:

C#
void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs

    //Redirect to custom error page and if required pass error code and error message to the custom error page using query string or some other mechenism.
    Response.Redirect("CustomErrorPage.aspx");

}
 
Share this answer
 
try like this

in the Student class modify the method like this

C#
public bool IsStudentExist(string studentName)
    {
        Student exist = db.Students.FirstOrDefault(x => x.studentName == studentName);
        return exist != null;

    }


And in the page where you are calling that method to check the student exists ,
like this

C#
Student obj = new Student();
       bool isexist = obj.IsStudentExist("studendName");
       if (isexist)
           Response.Redirect("Error.aspx?Message=Student Exists");


Create an Error page as follows

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            <asp:Label ID="lblMessage" runat="server"></asp:Label>
        </h3>
    </div>
    </form>
</body>
</html>


C#
protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = Request.QueryString["Message"];
        }
 
Share this answer
 
Comments
RaviRanjanKr 22-Jan-14 13:27pm    
5+
Karthik_Mahalingam 22-Jan-14 20:34pm    
Thanks Ravi :)
Rahul VB 22-Jan-14 13:58pm    
and the "MOM"(Man of the Moment) award goes to Karthik....hahaha 5+
Karthik_Mahalingam 22-Jan-14 20:35pm    
Thanks once again Rahul :)
Member 10398773 24-Jan-14 8:40am    
This works like a charm....Thanks sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900