4

I want go get last record field value from my SPList with CAML query. Really code working but gives me following ERROR...

My Code:

void ddCarNumber_TextChanged(object sender, EventArgs e)
{
    try
    {

       SPWeb myWEB = SPContext.Current.Web;

        SPList myList = myWEB.Lists["WayBill"];

        SPQuery myQuery = new SPQuery();
        myQuery.Query = @"<OrderBy>
                            <FieldRef Name='Created' Ascending='FALSE' /> 
                        </OrderBy> 
                        <Where>
                            <Eq>
                                <FieldRef Name='CarNumber1'/>
                                <Value Type='Text'>" + ddCarNumber.Text + @"</Value>
                            </Eq>
                        </Where>";

           SPListItemCollection myItemCol = myList.GetItems(myQuery);

            if (myItemCol.Count > 0)
            {
                string myEndMil = myItemCol[0]["endMil"].ToString();

                string digitsOnly = String.Empty;
                foreach (char c in myEndMil)
                {
                    if (c >= '0' && c <= '9')
                    {
                        digitsOnly += c;
                    }
                }

                txtStartMil.Text = digitsOnly;
            }
            else
            {
                txtStartMil.Text = string.Empty;
            }
    }
    catch (Exception ex)
    {   
       lblError.Text = ex.ToString();
    }
}

Error is :

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. at Microsoft.SharePoint.SPListItemCollection.get_Item(Int32 iIndex) at Lirex.WayBillModule.ddCarNumber_TextChanged(Object sender, EventArgs e)

0

4 Answers 4

1

Try this below method:

void ddCarNumber_TextChanged(object sender, EventArgs e)
{
    try
    {

       SPWeb myWEB = SPContext.Current.Web;

        SPList myList = myWEB.Lists["WayBill"];

        SPQuery myQuery = new SPQuery();
        myQuery.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy><Where><Eq><FieldRef Name='CarNumber1'/><Value Type='Text'>" + ddCarNumber.Text + "</Value></Eq></Where>";

           SPListItemCollection myItemCol = myList.GetItems(myQuery);

            if (myItemCol.Count > 0)
            {
                if(myItemCol[0]["endMil"] != null)
                {
                    string myEndMil = myItemCol[0]["endMil"].ToString();

                    string digitsOnly = String.Empty;
                    foreach (char c in myEndMil)
                    {
                        if (Char.IsDigit(c))
                        {
                            digitsOnly += c;
                        }
                    }
                }
                txtStartMil.Text = digitsOnly;
            }
            else
            {
                txtStartMil.Text = string.Empty;
            }
    }
    catch (Exception ex)
    {   
       lblError.Text = ex.ToString();
    }
}

Hope this will help you!

3

Here string myEndMil = myItemCol[0]["endMil"].ToString(); you are assuming you have gotten at least one result back. That might not be the case!

You should check first that myItemCol.Count > 0

There is another possible issue at the same line (string myEndMil = myItemCol[0]["endMil"].ToString();)

You are requesting a column endMil that you might not have.

1
  • see my code, maybe this ?? Commented Jul 5, 2016 at 7:59
2

Can you try putting <RowLimit>1</RowLimit> in the Query. The row limit will return only the one last item.

2
  • paste this in my query, please :) Commented Jul 5, 2016 at 8:19
  • put myQuery.RowLimit = 1; before SPListItemcollection line Commented Jul 5, 2016 at 8:24
1

Try this code. Added some safe checks

void ddCarNumber_TextChanged(object sender, EventArgs e)
{
    try
    {    
        SPWeb myWEB = SPContext.Current.Web;

        SPList myList = myWEB.Lists.TryGetList("WayBill");
        if(myList != null)
        {
           SPQuery myQuery = new SPQuery();
           myQuery.Query = @"<OrderBy>
                            <FieldRef Name='ID' Ascending='FALSE' /> 
                        </OrderBy> 
                        <Where>
                            <Eq>
                                <FieldRef Name='CarNumber1'/>
                                <Value Type='Text'>" + ddCarNumber.Text + @"</Value>
                            </Eq>
                        </Where>";

            myQuery.RowLimit = 1; 
            SPListItemCollection myItemCol = myList.GetItems(myQuery);

            if (myItemCol != null && myItemCol.Count > 0)
            {
                string digitsOnly = String.Empty;
                string myEndMil = String.Empty;
                if(myItemCol[0]["endMil"] != null)
                {
                   myEndMil = Convert.ToString(myItemCol[0]["endMil"]);    

                   foreach (char c in myEndMil)
                   {
                       if (c >= '0' && c <= '9')
                       {
                           digitsOnly += c;
                       }
                   }
                }
                txtStartMil.Text = digitsOnly;
            }
            else
            {
                txtStartMil.Text = String.Empty;
            }
        }
    }
    catch (Exception ex)
    {   
       lblError.Text = ex.ToString();
    }
}
  • While fetching a list use TryGetList like this myWEB.Lists.TryGetList("WayBill");
  • Use ID Column possibly when performing a ORDER BY operation. Since ID is indexed column, query execute faster.
  • You must always do a null check when query is fired and data is assigned to SPListItemCollection
  • You should try to keep practice of using Convert.ToString() instead of .ToString() for ex Convert.ToString(myItemCol[0]["endMil"]); and not myItemCol[0]["endMil"].ToString()
  • Also check if column exists or not. if(myItemCol[0]["endMil"] != null)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.