Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating dataset from outlook inbox 1

Status
Not open for further replies.

arkadia93

Programmer
Oct 19, 2006
110
GB
I have a collection of objects (oItems) that I want to use to populate a dataset. Basically I want to take several elements from oItems (such as SenderName, Subject and Body) and use them to populate separate columns of a dataset, and then use the dataset to populate a gridview.
Can anybody help me out with this?


//Create Outlook application
Outlook.Application oApp = new Outlook.Application();

//Get MAPI namespace
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

oNS.Logon("name", "password", false, true);

//Get Messages collection of Inbox
Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

Outlook.Items oItems = oInbox.Items;

int intTotalNumberOfItemsInInbox = oItems.Count;

//Get unread e-mail messages
oItems = oItems.Restrict("[Unread] = true");
int intTotalUnread = oItems.Count;


//Loop through each unread message
Outlook.MailItem oMsg;
int i;

for (i = 1; i < oItems.Count; i++)
{
oMsg = (Outlook.MailItem)oItems.Item(i);

txtSenderName.Text = oMsg.SenderName.ToString();
txtSubject.Text = oMsg.Subject.ToString();
txtReceivedTime.Text = oMsg.ReceivedTime.ToString();
txtBody.Text = oMsg.Body.ToString();
}

//Log off
oNS.Logoff();

//Clean up
oApp = null;
oNS = null;
oItems = null;
oMsg = null;
 
You don't need to use a DataSet as a DataTable will suffice for what you want to do. Here's an example of how you can populate a DataTable:
Code:
        Dim dt As New DataTable
        Dim dr As DataRow

        dt.Columns.Add("Column1")
        For i As Integer = 0 To 9
            dr = dt.NewRow
            dr(0) = i
            dt.Rows.Add(dr)
        Next


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry, I'm not quite following what you are doing. Would this be how you would add the first column, SenderName? And would you repeat for all the other columns?

DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add("SenderName");
Outlook.MailItem oMsg;
int i;

for (i = 1; i < oItems.Count; i++)
{
dr = dt.NewRow();
dr[0] = oMsg.SenderName.ToString();
dt.Rows.Add(dr);
}

 
Yes, that's pretty much it. Just add whatever columns you need and then add the data to them in the loop. You can also reference the column by name e.g. In my example above,
Code:
dr(0) = i
could also be written as
Code:
dr("Column1") = i



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I'm thinking that it should be something like this but I keep getting errors :

DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add("SenderName");
dt.Columns.Add("Subject");
dt.Columns.Add("ReceivedTime");
dt.Columns.Add("Body");

Outlook.MailItem oMsg;
int i;

for (i = 1; i < oItems.Count; i++)
{
dr = dt.NewRow();
dr["SenderName"] = oMsg.SenderName.ToString();
dr["Subject"] = oMsg.Subject.ToString();
dr["ReceivedTime"] = oMsg.ReceivedTime.ToString();
dr["Body"] = oMsg.Body.ToString();
dt.Rows.Add(dr);
}
 
Remove the reference to the "i" variable when you are setting the values e.g.
Code:
dr("SenderName") = oMsg.SenderName.ToString()


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Almost got it sorted, but when I set i = 0 in the for loop I get 'array out of bounds', but when I set i = 1, I miss one of the records...any ideas?

DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add("SenderName");
dt.Columns.Add("Subject");
dt.Columns.Add("ReceivedTime");
dt.Columns.Add("Body");

Outlook.MailItem oMsg;
int i;

for (i = 1; i < oItems.Count; i++)
{
oMsg = (Outlook.MailItem)oItems.Item(i);

dr = dt.NewRow();
dr["SenderName"] = oMsg.SenderName.ToString();
dr["Subject"] = oMsg.Subject.ToString();
dr["ReceivedTime"] = oMsg.ReceivedTime.ToString();
dr["Body"] = oMsg.Body.ToString();
dt.Rows.Add(dr);
}

GridView1.DataSource = dt;
GridView1.DataBind();



 
If i starts at zero, then the count will always be one higher than the number of loops you need to do. Either use oItems.Count - 1 or use an enumerator to loop through the records.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I just changed the for loop as below :

for (i = 1; i < oItems.Count + 1; i++)



Thanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top