I am working on outlook add-in where I need to set a custom header. I am using VS2010 for my development.
I am trying with the following code but it doesn't seems to be working.
private void AddUserProperty(Outlook.MailItem mail, string folderEmailId)
{
Outlook.PropertyAccessor mailPropertyAccessor = null;
try
{
if (string.IsNullOrEmpty(folderEmailId))
return;
mailPropertyAccessor = mail.PropertyAccessor;
mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId", folderEmailId);
mail.Save();
try
{
MessageBox.Show("Existing :" + mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId"));
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (System.Exception ex)
{
Logger.Error(ex);
MessageBox.Show(ex.Message);
}
finally
{
if (mailPropertyAccessor != null)
Marshal.ReleaseComObject(mailPropertyAccessor);
}
}
After saving the mail item, I am trying to fetch the same item for verification, but it's throwing an exception saying the property not found.
I don't see a problem with your code, although getting a reference directly to the PropertyAccessor is unnecessary. Try:
string prop = "http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId";
mail.PropertyAccessor.SetProperty(prop, folderEmailId);
mail.Save();
Related
I'm trying to show a Duplicate Record error message in an WebAPI service. I'm using .net5 and mysql. I cannot seem to find a way to pull the duplicateEntry message from the exception response. I'd like to key in on the ErrorCode field and use this to tailor a response to the user. I can see the Message property, but cannot figure out how to access the innerException.
{
try
{
module.Id = Guid.NewGuid();
await _moduleRepository.Add(module);
await _uow.CompleteAsync();
return true;
}
catch (DbUpdateException ex)
{
logger.LogWarning("Module - Add Error: " + ex.Message);
return false;
}
}
You may try to catch an Exception object which has a InnerException attribute. Also, you may also check DbEntityValidationException class.
Reference: link
Found something that worked. Made a helper function, but this is highly coupled to Mysql. Had to check the InnerException to see if it was of type MsyqlException.
{
public static string GetExceptionMessage(Exception ex)
{
var message = ex.Message;
var innerException = ex.InnerException;
if (innerException != null && (innerException.GetType() == typeof(MySqlException)))
{
var dbException = (MySqlException)innerException;
if(dbException.ErrorCode == MySqlErrorCode.DuplicateKeyEntry)
{
return ErrorMessages.RecordExists;
} else
{
return ErrorMessages.UnknownError;
}
}
return message;
}
}
I'm building a wpf application with a database in entity framework code first.
I allow the user to delete items in database, but when item is linked to other data, an exception will be thrown.
This is what I had originally
try
{
//delete item
}
catch (Exception ex)
{
MessageBox.Show(ex.Message)
}
This is working, but the message displayed isn't so much user firendly. So I tried to create a custom exception
public class CustomException : Exception
{
public CustomException (string Message)
{
Message = "Item can't be deleted";
}
}
And I've modified my catch
catch (CustomException ex)
{
MessageBox.Show(ex.Message)
}
But this is not showing the messagebox with my message, but it's stopping my application for a DbUpdateException.
Is there a way to customize the message for this type of exception?
Modifying the catch won't change how the framework throws an exception. You should just catch the DbUpdateException and show a message that is appropriate:
catch (DbUpdateException ex)
{
MessageBox.Show("item can't be deleted");
}
I am writing a plug in to pull as much data out of a CAD as possible. The main issue I am having right now is when I try to access StartPoint.X, for example, the script fails without catching an exception with "FATAL ERROR: Unhandled Access Violation Reading 0xffffffff at d8e176b4h." Depending on what I try to access, the memory location and whatever the second number change. Example:
foreach (Objects o in globalListOfObjs)
{
string type = o.obj.GetType().ToString().Split('.').Last();
if (type == "Line")
{
try
{
Line l = (Line)o.obj;
if (l != null)
{
MessageBox.Show("Not Null!");
MessageBox.Show(l.StartPoint.X.ToString());
}
//listOfLines.Add(new LinkLines(lx1, ly1, lx2, ly2, Guid.NewGuid()));
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
}
}
An object can be safely accessed only if the transaction used to open the object is still active. If you need to store a reference to an object, store the ObjectId and start a new transaction if you need to access the object properties.
Does your code is running in the main thread? AutoCAD does not support multithreading.
Try something like this:
public void getStartPoint(Transaction oTr, ObjectId oId)
{
try {
Line oLn = (Line)oTr.GetObject(oId, OpenMode.ForRead);
if (oLn != null) {
Interaction.MsgBox(oLn.StartPoint.X.ToString);
}
} catch (System.Exception ex) {
Interaction.MsgBox(ex.StackTrace, (MsgBoxStyle)MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, ex.Message);
}
}
So, I am getting an exception in Stored Procedure whose error code is 547. I have created a data diagram in sql server, where I have defined the relationships.when I run the any delete SP I get an error saying FK conflict, which is right. My problem is how do I get the number(i.e 547) in my C# code.
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
I sthere any way where I can get this 547 code in my C# code.? Like
ex.somethin (which gives me the error message's code).
Try This.
try
{
}
catch(SqlException ex)
{
lblMessage.Text = ex.Message;
}
Multiple catches can be used:
try
{
}
catch(SqlException sqlex)
{
if(sqlex.Number ==547)
{
//code
}
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}
You can try using Elmah library (Error Logging Modules And Handlers)
Here is a step by step tutorial on how to use it: http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/logging-error-details-with-elmah-cs
For more details http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx
try
{
...
...
}
catch(SqlException ex)
{
lblMessage.Text = ex.Message;
}
Am just wondering how to workaround my scenario,here is my code
try
{
bool b;
foreach (string file in files)
{
#region donloadfiles
if (b = file.Contains(story))
{
try
{
logger.Info("calling xml creation Method");
baseMeta(story, XML);
logger.Info("XML created");
}
catch (Exception ex)
{ logger.Error(ex.Message); throw; }
logger.Info("calling Download Method");
Download(file, story, xml, program);
logger.Info("Download Method processed successfully");
}
#endregion
}
}
catch (Exception ex)
{ logger.Error(ex.Message); throw; }
As promised,here is my main method contains try catch block
try
{
//update the status here in DB
Status = "Closed";
Update(status);
}
catch (Exception ex)
{
Status = "Failed";
Update(status);
break;
}
i have directory "for eg:C:\" my getlist method grab all the record and pass it to my foreach loop "foreach (string file in files)" then i have a condition
if (b = file.Contains(story)) to check any of my file have the name "story" then do some logic inside.this thing works good.
Now what am trying to do is,if none of the files are matching then i have to forcefully call the catch and throw to the main catch,am doing some logic update in my main catch.
Someone please advice me how can i workaround this scenario.
Thanks in advance
what am trying to do is,if none of the files are matching then i have
to forcefully call the catch and throw to the main catch,am doing some
logic update in my main catch.
bool anyMatch = false;
foreach (.....)
{
if (b = file.Contains(story))
anyMatch = true;
}
if(!anyMatch)
throw ... //this will be caught by main catch
Keep a bool variable outside foreach loop. Set it to true if any file matches. if it is false at the end of foreach loop throw exception
Usher, using exceptions to manage process flow is a BAD idea!
Exceptions are there to manage errors, and not to handle expected conditions in your code's execution.
A much better way (in the long run:trust me on this) would be to return some value out of your method when none of the files match instead of throwing an exception.
Something like:
#region donloadfiles
if (b = file.Contains(story))
{
try
{
logger.Info("calling xml creation Method");
baseMeta(story, XML);
logger.Info("XML created");
}
catch (Exception ex)
{ logger.Error(ex.Message); throw; }
logger.Info("calling Download Method");
Download(file, story, xml, program);
logger.Info("Download Method processed successfully");
}
else return "no matches found";
#endregion
and handle the process from there once you've gotten the "no matches found" value in the place that called this method.