Solved

index out of bounds error

Posted on 2011-03-15
6
763 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 26
Updating a single record in forEach using C#/Entity 7 26
Record locking on classic ASP 3 40
Need help with a query 14 39
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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