Solved

index out of bounds error

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

825 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