Solved

index out of bounds error

Posted on 2011-03-15
6
760 Views
Last Modified: 2012-05-11
I have a webform that is making a call to the Salesforce API and returning event records.  I am then looping thru the events as a test to spit out the values on the page.

I am getting the index out of bounds error.

See my code below.

The total number of event records is 7693.

Thanks for any help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalesforceLibrary;
using SalesforceLibrary.sforce;

public partial class _Default : System.Web.UI.Page 
{
    private String username;
    private String password;
    private String sessionId;
    private String serverUrl;
    private SforceService proxy;
    private QueryResult qr = null;
    private String soql = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        soql = "Select WhoId, Type, Subject From Event";

        proxy = new SforceService();
        if (Request.QueryString["sessionId"] != null)
        {
            sessionId = Request.QueryString["sessionId"].ToString();
            serverUrl = Request.QueryString["url"].ToString();
        }
        else
        {
            username = "xxxxxxx_xxxx@xxx.xxx";
            password = "xxxxxxxxx";
        }

        if (login())
        {
            qr = proxy.query(soql);

            if (qr.size > 0)
            {
                Response.Write("********************* " + qr.size);
                for (int i = 0; i <= qr.size; i++)
                {
                    Event ev = (Event)qr.records[i];
                    Response.Write("Event Name = " + ev.Who);
                    Response.Write("<br />");
                    Response.Write("Event Type = " + ev.Type);
                    Response.Write("<br />");
                    Response.Write("Event Subject = " + ev.Subject);
                    Response.Write("<br />");
                }
            }
            else
            {
                Response.Write("No Records returned.");
            }
        }
    }

    protected bool login()
    {
        if (sessionId != null)
        {
            try
            {
                proxy.Url = serverUrl;
                proxy.SessionHeaderValue = new SalesforceLibrary.sforce.SessionHeader();
                proxy.SessionHeaderValue.sessionId = sessionId;
            }
            catch (Exception ex)
            {
                Response.Write("Error logging in with session: " + ex.Message);
                return false;
            }
            return true;
        }
        else
        {
            SalesforceLibrary.sforce.LoginResult lr = proxy.login(username, password);

            if (!lr.passwordExpired)
            {
                proxy.Url = lr.serverUrl;
                proxy.SessionHeaderValue = new SalesforceLibrary.sforce.SessionHeader();
                proxy.SessionHeaderValue.sessionId = lr.sessionId;
            }
            else
            {
                Response.Write("Your password expired.");
                return false;
            }
            return true;
        }
    }
}

Open in new window

0
Comment
Question by:-Dman100-
6 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35143728
What line is throwing the exception?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35143736
The index goes on the qr array.  Not sure about the rest, but this gets you the first "record" in the array, and casts it as an event.

             
       Event ev = (Event)qr[i].records[0];

Open in new window

0
 
LVL 3

Accepted Solution

by:
ccwill88 earned 250 total points
ID: 35143739
i must less qr.size,because it start at 0
 for (int i = 0; i <= qr.size; i++)
==> for (int i = 0; i < qr.size; i++)
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 35143756
your for loop starts at zero and loops until it exceeds qr.size, you need to make it stop before it reaches qr.size as the array starts at zero so the last record returned will be qr.size - 1
change this :

for (int i = 0; i <= qr.size; i++)

to

for (int i = 0; i < qr.size; i++)
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 35143914
As jacko72 said, stop the for loop increment (i) before it reaches the qr.size (i < qr.size).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now