Solved

index out of bounds error

Posted on 2011-03-15
6
759 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

22 Experts available now in Live!

Get 1:1 Help Now