Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvice with custom exceptions
    text
    copied!<p>I created a custom exception</p> <pre><code>public class InvalidUsernameException : ApplicationException { public InvalidUsernameException() { } } </code></pre> <p>After that I throw this exception in a method</p> <pre><code>public static DataTable GetTableForApproval() { using (var connection = Utils.Database.GetConnection()) using (var command = new SqlCommand("SELECT [UserID], [Username], [Email], [Role], [Date] FROM [Users] WHERE [Role] = @role", connection)) { command.Parameters.AddWithValue("@role", "Waiting"); using (var reader = command.ExecuteReader()) { if (reader == null || !reader.HasRows) throw new NoFormsForAuthenticaionException(); var table = new DataTable(); table.Columns.Add("UserID", typeof(int)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Email", typeof(string)); table.Columns.Add("Role", typeof(string)); table.Columns.Add("Registration date", typeof(DateTime)); while (reader.Read()) table.Rows.Add((int)reader["UserID"], (string)reader["Username"], (string)reader["Email"], (string)reader["Role"], (DateTime)reader["Date"]); return table; } } } </code></pre> <p>And when I use these method I catch the exception </p> <pre><code>try { GvApproveUser.DataSource = Authentication.GetTableForApproval(); GvApproveUser.DataBind(); } catch (NoFormsForAuthenticaionException) { Error.HandleError("There is no forms for approval"); } </code></pre> <p>This is the code behind the Error page</p> <pre><code>public partial class Error { public static string GetUrl(string message) { return string.Format("~/Error.aspx?errorMessage={0}", message); } public static void HandleError(string message) { HttpContext.Current.Response.Redirect(GetUrl(message), false); } protected override void OnPreRender(System.EventArgs e) { base.OnPreRender(e); LblError.Text = ErrorMsg; } private string ErrorMsg { get { return Request["errorMessage"] ?? string.Empty; } } } </code></pre> <p>Is there a easier way to post this error message? Can I do this without violating the rules of 3-tier architecture? Is there another way? </p> <pre><code>public class InvalidUsernameException : ApplicationException { public InvalidUsernameException() { Error.HandleError("There is no forms for approval"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload