I am using asp.net and I am always struggling for a neat way to handle errors and if there are any, to pass the errormessage to the user. For example, I have an User class, UserManager class and a Database class. Imagina I want to show all the users, show I call the method GetAllUsers from UserManagerwhich returns a list of User objects. This method creates a Database object and calls the method ExecuteQuery(string query).
EDIT: And now my issue, imagine something goes wrong in the ExecuteQuery() method (failed to open database). I want to notify the user someting went wrong when opening the database connection. How should I do this and handle this a neat way?
EDIT 2:
Would you do it something like this? Or otherwise?
public class Database()
{
private string _Error;
// property error (only get)
private void Open()
{
try
{
// Open DB
// Fails because of error
}
catch(Exception ex)
{
_Error = ex.Message;
}
}
public DataSet ExecuteQuery(string query)
{
try
{
Open();
// Execute query
// return dataset
}
catch(Exception ex)
{
_Error = ex.Message;
return null;
}
}
}
public class UserManager ()
{
private string _Error;
// Get property for Error
public List<User> GetAllUsers ()
{
Database db = new Database()
Dataset DS = db.ExecuteQuery("my query goes here");
(if DS == null)
{
_Error = db.Error;
return null;
}
}
}
In user interface on a click event:
protected void onClick_event(args)
{
Usermanager userman = new UserManager();
List<User> users = userman.GetAllUsers();
if(users == null)
{
// make a error panel visible
pnlError.Visible = true;
lblError.Text = userman.Error
}
}
Is this a good approach?