Attempting to get DASL property value from the Outlook table inbox - c#

I'm attempting to read the DASL value PR_LONGTERM_ENTRYID_FROM_TABLE 0x66700102 mentioned in this thread -
get outlook mailitem for message taken from outlook table
The issue I'm having is with the following line in the code below from the full example below-
string ltEntryid = (string)nextRow["http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString()];
It throws an exception "Cannot convert type 'byte[]' to 'string'"
I might be going about this the wrong way so I'm looking for some advice. I can read all the other tables rows fine (Example- "EntryID(short term), MessageClass, Unread, SenderEmailType).
const string unReadfilter = "[UnRead] = true";
Outlook.Table table = folder.GetTable(unReadfilter, Outlook.OlTableContents.olUserItems);
// Remove the default column set.
table.Columns.RemoveAll();
// Add columns to the table
table.Columns.Add("Unread");
table.Columns.Add("EntryID");
table.Columns.Add("MessageClass");
table.Columns.Add("SenderEmailType");
table.Columns.Add("SenderEmailAddress");
// PR_LONGTERM_ENTRYID_FROM_TABLE
table.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString());
// sort table
table.Sort("Unread", true);
while (!table.EndOfTable)
{
Outlook.Row nextRow = table.GetNextRow();
bool unRead = (bool)nextRow["Unread"];
Debug.WriteLine(unRead);
string msgClass = (string)nextRow["MessageClass"];
Debug.WriteLine(msgClass);
string eId = (string)nextRow["EntryID"];
Debug.WriteLine(eId);
string sEaddr = (string)nextRow["SenderEmailAddress"];
Debug.WriteLine(sEaddr);
string sEtype = (string)nextRow["SenderEmailType"];
Debug.WriteLine(sEtype);
// PR_LONGTERM_ENTRYID_FROM_TABLE ***Exception with the following line***
string ltEntryid = (string)nextRow["http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString()];
Debug.WriteLine(ltEntryid);
if (msgClass.Equals("IPM.Note"))
{
//write to string list
dailyMiInboxList.Add(unRead.ToString());
dailyMiInboxList.Add(msgClass);
dailyMiInboxList.Add(eId);
dailyMiInboxList.Add(sEaddr);
dailyMiInboxList.Add(sEtype);
dailyMiInboxList.Add(sEaddr);
dailyMiInboxList.Add(ltEntryid);
}
}

PT_BINARY property is returned as an array of byte, but you are casting it to a string. If you want to convert it to a hex string, use MAPIFolder.PropertyAccessor.BinaryToString().

OK, I figured this out with Dmitry's help.
First when adding this dasl property to the table -
// PR_LONGTERM_ENTRYID_FROM_TABLE
table.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString());
I should've not included the tostring so it should be written as follows-
table.Columns.Add(#"http://schemas.microsoft.com/mapi/proptag/0x66700102");
Next in the While loop, to convert the PT_BINARY property from an array of bytes use this to convert the row-
string PR_LONGTERM_ENTRYID_FROM_TABLE = "http://schemas.microsoft.com/mapi/proptag/0x66700102";
string ltEntryId = (string)nextRow.BinaryToString(PR_LONGTERM_ENTRYID_FROM_TABLE);
Debug.Print(ltEntryId);
This link was very helpful
In particular these comments-
• The value returned for a given column representing a binary (PT_BINARY) value depends on whether a built-in property name or a schema name was used to specify the Column. For explicit built-in property names (such as EntryID), the Row(Column) value is returned as a string. For property names referencing a namespace that represent a PT_BINARY property, the Row(Column) value is returned as a byte array. Use Row.BinaryToString to convert the byte array to a string.

Related

Parsing string for object property

How do I convert "Account" in to
<Account>
or "Object Name" into
<Object with matching name>
I'm attempting to parse a string and replace values in a string with object properties.
This is similar to what we do with form letters we send out, you know the Dear [Customer Name], thank you for purchasing [Some Item]. In this case the fields in the letter aren't setup by me and I need a means of converting what is in the text block into an object property.
Currently I'm using a code in the string {value=x; id=y ;property=z} and running that through a switch case to convert it into an object. Then replacing the {} with the object properties.
for example "some random string {value=1; id=1; property=Name} continued random string"
I parse the string to locate {value=, on a hit it runs through a switch case, where on case 1: return Account(ID = 1). The I grab Account.Name
Once I have that I put it in a text box so the user can validate that it is correct before generating a final document to be sent out.
Is there a way to have {Object.Property} in the string then use reflection to convert that string value to the object value?
Get property value from string using reflection
I used the above in other instances, but that requires that I have the object.
public static List<string> GetClassProperties(string className, IEnumerable<string> propertiesToExclude = null)
{
Type theType = Type.GetType(className);
if (theType == null)
{
return null;
}
}
This'll get you the Object type, you do need the full namespace for it to work, can't just be "Account" would need to be Project.Models.Account

NSPasteboard in Xamarin.Mac

I am trying to get string from NSPasteboard in Xamarin.Mac, here is my code
NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;
string text = "";
Then what method of pasteboard do I use to store the string into text?
If you know you have string-based data on the pasteboard, you can use GetStringForType using the type of NSStringType.
Example:
string text = NSPasteboard.GeneralPasteboard.GetStringForType(NSPasteboard.NSStringType);
Returns a concatenation of the strings for the specified type from all the items in the receiver that contain the type.
Otherwise you can cycle through all the items on in the pasteboard using
PasteboardItems to return an array of NSPasteboardItem and review each item for the type that you are looking for.

Formatting a number string to add commas - c#

I know this question has been answered many times before however, I'm convinced the code I have is correct but isn't working correctly.
string total = ds.Tables[0].Rows[0][0].ToString();
string test = string.Format("{0:N}", total);
lbl_totalValue.Text = test;
This code isn't adding the commas into my value like it desire it to.
Can anyone see why?
When you put
string total = ds.Tables[0].Rows[0][0].ToString();
it means implicit G ("General") format string
string total = ds.Tables[0].Rows[0][0].ToString("G");
Do not format prematurely:
var total = ds.Tables[0].Rows[0][0]; // Value from table
string test = string.Format("{0:N}", total); // Format total with "N" format string
Your code is trying to format a string. If the DataTable contains a number you can pass the format specifier to ToString(), eg
var test=ds.Tables[0].Rows[0][0].ToString("N");
Or store the contents in a local variable and use String.Format :
var total = ds.Tables[0].Rows[0][0];
string test = string.Format("{0:N}", total);
If the datatable contains a string though, you'd have to parse it to a numeric type first
You have to use the string.Format with a number type, instead of string. In this case, the variable total is a string, it must be a number.
There are 8 overloads for the Strig.Format method. You are using this specific one: Format(String, Object) in which you pass a String value as argument of the second parameter. This is because you are using a string variable (total) to assign the value from the dataset in:
string total = ds.Tables[0].Rows[0][0].ToString();
Besides you are using .ToString() to retrieve it as a String.
If you are using SQL Server as data source to your ds dataset and you are certain about the SQL data type then you can assign that value directly to a variable with the corresponding C# type. To put it in a different way, SQL data types are mapped to C# data types.
If you are not sure about the C# data type of ds.Tables[0].Rows[0][0] then you could simply do the following:
Object total = ds.Tables[0].Rows[0][0];
string test = string.Format("{0:N}", total);
lbl_totalValue.Text = test;
And this way you literally use the Format(String, Object) overload of String.Format.

C# - Returning a single value (any datatype) from a DataTable as a string

I've loaded a table from excel in C# using the ExcelDataReader package, however I'm struggling to write a method which returns the value of a cell as a string, based on the row number and column name.
I have a solution for a method that works if there are just strings in the DataTable...
public string ReadData(int rowNum, string columnName)
{
DataRow row = table.Rows[rowNum];
string value = row.Field<string>(columnName);
return value;
}
(the DataTable variable 'table' is loaded elsewhere)
..however I would like also like to be able to return numbers and dates (as strings).
I feel I might need to edit this part somehow...
string value = row.Field<string>(columnName);
But I don't know how to make it so it pulls back whatever value and type is in that field and returns a string.
Many thanks!
You can simply use the by-name-indexer and call ToString():
string value = row[columnName].ToString();
You probably should check for null values:
string value = (row[columnName] ?? string.Empty).ToString();
You could use, DataRow name-indexer returns an object on which we can call .ToString() to convert it to string.
return row[columnname].ToString();
Use
string value = Convert.ToString(row[columnName]);
Using Convert.ToString() is preferrable because it does not only check for null but also for DBNull, which is the value of the column when accessing a real database where the column has no content.
In C# 6.0:
return row[columnName]?.ToString();

Can't convert values

Im trying to convert the Id field to send it to the DB but in the DB the type is bigInt:
var ID_Inscricao = (Label)row.FindControl("Label1");
How do I convert this to a type that is compatible with bigInt? I have tried cast and convert but neither worked.
You need to convert the value in the label's Text property to an integer of some sort:
string labelText = ((Label)row.FindControl("Label1")).Text;
var ID_Inscricao = Convert.ToInt64(labelText);
Beware that this could throw an exception if the text value is not a number.
You will have to take the Text property and convert that. You can't cast a control to a value.
Hint: Use the appropriate TryParse.
This may help you,
var ID_Inscricao = (Label)row.FindControl("Label1");
var ID_Inscricao_val = parseInt(ID_Inscricao.innerHTML);

Categories

Resources