Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Getting Error When tried to create web part to get all items from

  Asked By: Heriberto    Date: Nov 13    Category: Sharepoint    Views: 2102

Namespace used:

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList states = currentWeb.Lists["States"]; //If this line commented then not
getting the error


System.ArgumentException: Value does not fall within the expected range. at
Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean
bThrowException) at Microsoft.SharePoint.SPListCollection.get_Item(String
strListName) at StatesAndCities.Page_Load(Object sender, EventArgs e); Message:
Value does not fall within the expected range.
Please anyone help me, why i am getting this error, am i doing wrong. Can I get
any code to get all items (columns) from a list.



2 Answers Found

Answer #1    Answered By: Liana Alston     Answered On: Nov 13

That is old version 2003 code. This is a better way to do it.

SPWeb currentWeb = SPContext.Current.Web;
SPListCollection lists = currentWeb.lists;

SPList states = lists["States"]; //If this line  commented then not
getting the error

This will work assuming that the Title Field of the list  is set to

Answer #2    Answered By: Daamodar Kolhe     Answered On: Nov 13


gvCities.DataSource = spListItemCollection.GetDataTable();

That's it. It gets the data from Cities list  and binds to Gridview "gvCities".
It will give error  in WebPart Preview, but if you add this web  part in the
concerned website-list-cutsom page, it works...