2013-09-30 1 views
0

List 객체를 사용하여 사용자 속성을 저장하고 있습니다.인덱스가 범위를 벗어났습니다. 음수가 아니어야하며 List에있는 콜렉션의 크기보다 작아야합니다. <string>

내 코드입니다 : 나는이 프로그램을 실행할 때 내가 위에서 언급 한 오류가 발생하고

string department=string.Empty; 
List<string> otherDepartments = new List<string>(); 
int no; 
string result = string.Empty; 

string queryText = string.Empty; 
using (SPSite siteCollection = SPContext.Current.Site) 
{ 
    using(SPWeb site = siteCollection.OpenWeb()) 
    { 
     SPSecurity.RunWithElevatedPrivileges(delegate() 
     { 
       SPUser spUser = site.CurrentUser;       
       SPServiceContext context = SPServiceContext.GetContext(siteCollection); 
       UserProfileManager upm = new UserProfileManager(context); 
       UserProfile profile = upm.GetUserProfile(spUser.LoginName); 
       department = profile["oiplbDepartment"].Value.ToString(); 

       UserProfileValueCollection otherDept = profile["oiplbOtherDepartments"]; 

       if (otherDept.Count != 0) 
       {       
        foreach (var prop in otherDept) 
        { 
         otherDepartments.Add(prop.ToString()); 
        } 
       } 
       Label1.Text = department + " Ohter Departments " + otherDepartments; 
      }); 

      SPList list = site.Lists["Events"]; 
      SPListItemCollection itemColl; 
      SPQuery query = new SPQuery(); 
      no = 1 + otherDepartments.Count; 
      for (int i = 1; i <= no; i++) 
      { 
       if (no == 1) 
       { 
        result = "<or> <Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>"+department+"</Value></Eq>"+"</or>"; 
        break; 
       } 
       else if (i == 2) 
       { 
        result = "<or> <Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>" + department + "</Value></Eq>" + 
        "<Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>" + otherDepartments[i-1] + "</Value></Eq>"; 
       } 
       else if(i >= 3) 
       { 
        result = generateOr(result,otherDepartments[i-1]); 
       } 
     }         
     queryText = "<where>"+result +"</Where>"; 
     query.Query = queryText; 
     itemColl = list.GetItems(query); 
     Grid.DataSource = itemColl.GetDataTable(); 
     Grid.DataBind(); 
    } 
} 

public static string generateOr(string temp, string value) 
{ 
    string r = "<or>" + temp + " <Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>" + value + "</Value></Eq> </or>"; 
    return r; 
} 

.

그렇지 않으면 속성을 사용할 수있는 경우에만 값을 삽입합니다.

하지만 오류가 발생하는 이유는 무엇입니까?

+0

어떤 라인에서 좋습니까? – Arran

+0

이 줄을 변경해보십시오 : for (int i = 1; i <= no; i ++)'for (int i = 1; i

+0

아니요, for (int i = 1, k = 0; i <= 아니오; i ++, k ++ 0')와 같은 for 루프를 변경하고'k '를 통해리스트 인덱스에 액세스 할 수 있습니까? –

답변

4

그것 때문에

no = 1 + otherDepartments.Count; 

변화의 그것은 당신이 i == no 때까지 목록에 for-loop 루프를 항목에 액세스하기 전에 i에서 1을 뺀다하더라도

no = otherDepartments.Count; 

합니다. 그래서 당신도 바꿀 수있다 i <= no to i < no

for (int i = 1; i < no; i++) 
+0

감사합니다. 질문에 최소한 값 1이 있어야합니다. 그러면 어떻게해야합니까? 이것은 사용자가 적어도 하나의 부서에 속하기 때문입니다. –

+0

@ RiyazKalva : '당신도 변할 수 있습니다'부분을 읽었습니까? –

관련 문제