Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

index out of bounds error

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

Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

What line is throwing the exception?
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

ASKER CERTIFIED SOLUTION
Avatar of ccwill88
ccwill88
Flag of Taiwan, Province of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As jacko72 said, stop the for loop increment (i) before it reaches the qr.size (i < qr.size).