try like this
in the Student class modify the method like this
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
Student obj = new Student();
bool isexist = obj.IsStudentExist("studendName");
if (isexist)
Response.Redirect("Error.aspx?Message=Student Exists");
Create an Error page as follows
<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>
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = Request.QueryString["Message"];
}