I'm having difficulty accessing the values of the data property of the Exception class.
try
{
string strReasonCode = String.Empty;
string strReasonDescription = String.Empty;
string strDuration = String.Empty;
string strActive = String.Empty;
if (ReasonCodeDT.Rows.Count != 1)
{
string strPracticeID = PracticeID.ToString();
string PracticeName = getSingleStringResultFromSQL("SELECT @result = PracticeName FROM Practice WHERE PracticeID = /'" + strPracticeID + "/'");
string RecordCount = ReasonCodeDT.Rows.Count.ToString();
int UserID;
Int32.TryParse(au.UserID, out UserID);
Exception ex = new Exception("Database Returned " + RecordCount + " Records for ReasonCodeID " + ReasonCodeID + " for Practice " + PracticeName + " (" + strPracticeID + ")");
ex.Data.Add("UI Error Message", "Whoops! Something went wrong there. Please call (555)555-5555" +
"and notify them something is wrong with the Encounter Reason: " + ReasonCodeID);
throw ex;
}
else
{
}
}
catch
{
pnlError.Visible = true;
lblErrorMessage.Text = ex.Data["UI Error Message"];
}
This gives me a compiler error message:
Error 117 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
All the references I can find access that information by using foreach
loops specifying the type as DictionaryEntry
but that isn't necessary/ desirable for this instance:
How do I retrieve a single item from that dictionary?
UPDATE
In reference to @dbc's suggestion of creating a custom exception class, here is a link providing more detail as to why I didn't do so.
https://blogs.msdn.microsoft.com/jaredpar/2008/10/20/custom-exceptions-when-should-you-create-them/
Feel free to comment as to why an exception class would be better than using the data property of the base exception class.