I am a new ASP.net developer and I am trying to do add proper exception handling in my simple 3-tier web-based application. By following this post, I have done, for example, the following in the Data Access Layer (DAL) and the User Interface (UI):
DAL:
public IEnumerable<Survey> getData()
{
List<Survey> surveysList = new List<Survey>();
try
{
using (ItemsDBEntities context = new ItemsDBEntities())
{
surveysList = (from survey in context.Surveys
select new Survey()
{
ID = survey.ID,
startDate = survey.StartDate,
EndDate = survey.EndDate,
Description = survey.Description
}).ToList();
}
}
catch (EntityException ex)
{
//something wrong about entity
throw new ConnectionFailedException(ex);
}
catch (Exception ex)
{
//Don't know what happend...
}
return surveysList;
}
Code-Behind of UI:
private void bindGrid()
{
Survey survey = new Survey();
try
{
GridView1.DataSource = survey.getData();
GridView1.DataBind();
GridView1.Visible = true;
}
catch (ConnectionFailedException)
{
Label1.Text = "There was a problem accessing the database, please try again.";
}
}
However, I am still getting a red line under
ConnectionFailedException
in each tier and I don't know why, and it gave me the following error:
The type or namespace name 'ConnectionFailedException' could not be found (are you missing a using directive or an assembly reference?)TestWebsite\App_Code\DAL\Survey.cs
How can I fix this thing? I don't want to create a class for each exception type that I am going to throw it like what I have done so far. Could you please provide me with a help and example if possible?