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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating datatable 2

Status
Not open for further replies.

arkadia93

Programmer
Oct 19, 2006
110
GB
I am trying to populate a data table with a collection of Outlook mail objects, but I keep getting the error 'cannot apply indexing to an expression of type object'. Can anybody help me out?

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);
}
 
Got it nearly 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.

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 you are trying to add all records, and you need to start looping through the collection from i = 1, then wouldn't you want to start your for loop like this?

Code:
 for (i = 1; i [b]<=[/b] oItems.Count; i++)

I think you are missing the last item, not the first. <= should take care of that for you.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Just as a pertinent aside, instead of;
Code:
        int i;
        for (i = 1; i < oItems.Count; i++)
        {
                //Doing stuff here...
        }

...you can use;
Code:
        for (int i = 1; i < oItems.Count; i++)
        {
                //Doing stuff here...
        }

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
...that whole dangerous early morning brain issue, see post by AlexCuse.

Just as a pertinent aside, instead of;
Code:
        int i;
        for (i = 1; i <= oItems.Count; i++)
        {
                //Doing stuff here...
        }

...you can use;
Code:
        for (int i = 1; i <= oItems.Count; i++)
        {
                //Doing stuff here...
        }

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top