2013-04-26 1 views
0

내가 파괴 할 때 아래의 오류가 발생하고 생성하고이 데이터 테이블오류 : 색인이 범위를 벗어났습니다. 음수가 아니어야하며 콜렉션의 크기보다 작아야합니다. 매개 변수 이름 : 인덱스

RadioButton rdb2 = new RadioButton(); 
RadioButton rdb3 = new RadioButton(); 
rdb1 = (RadioButton)DataList1.Items[item.Id].FindControl("One"); 
rdb2 = (RadioButton)DataList1.Items[item.Id].FindControl("Three"); 
rdb3 = (RadioButton)DataList1.Items[item.Id].FindControl("Seven"); 

내가 뭐하는 거지 것은 내가 데이터베이스에서 데이터를 호출하고 새로운 데이터 테이블에 넣어하고 사용자 선택을 기반으로 내 DataList에 대해 DataScouce로 사용합니다.

이 오류를 해결하는 방법을 알려주세요. 해결책으로도 link을 보았지만 도움이되지 않았습니다.

+0

'item.Id' (값)이란 무엇입니까? – Arran

+1

당신의'DataList1'에 얼마나 많은'Item'이 있습니까? –

+0

데이터 테이블의 파괴 및 생성과 관련하여이 코드를 어디에 있습니까? – V4Vendetta

답변

0

Item.Id에있는 DataList.Items에있는 항목을 참조하려는 것 같습니다.

DataList.Items 요소가 포함되어 있는지 확인하고 Item.Id

item.Id 가정 (DataList.Items의 전체 요소 수보다 높지 그리고) 유효한 값이 있는지 유효한 정수는, 당신은 item.Id보다 작은 경우 확인할 수 있었다 컬렉션 :

RadioButton rdb2 = new RadioButton(); 

if (item.Id <= DataList.Items.Count()) { 
    rdb2 = (RadioButton)DataList1.Items[item.Id].FindControl("Three"); 
} 
+0

Microsoft는 http://support.microsoft.com/kb/836874에서이 문제에 대한 기사를 가지고 있지만 해결책이 없습니다. – Sana

+0

@ user1126060 - ADO.NET/MS의 버그는 아닙니다. 컬렉션에서 범위를 벗어난 요소에 액세스하려고합니다. 나는. 대런이라는 3 개의 항목 (0, 1, 2)을 가진 배열이 있다면.나는 물건을 얻을 수 있습니다. 대런 [0], 대런 [1], 대런 [2]. 그러나 Darren [100]을 수행하면 예외가 발생합니다 (배열에 3 개의 요소가 있기 때문에 100을 시도하면 범위를 벗어납니다). –

+0

DataList1에 어떤 크기도 지정하지 않았지만 페이지 당 6 개 항목의 페이지 매김을 부여했습니다. 첫 번째 항목 카테고리에는 다섯 개의 항목이 있으며 코드는 정상적으로 작동하지만 두 번째 항목 카테고리에는 9 개의 항목이 있으며 위의 오류가 표시됩니다. – Sana

0

DataList1.Items[item.Id] 널이 될 수 있으며, 또한 FindControl는 다른 유형으로 캐스팅 as를 사용하여, 널 더 나은 반환 할 수 있습니다. 지정된 타입이 아닌 경우 예외를 발생시키지 않습니다. 하지만 사용하기 전에 null을 확인해야합니다.

if((item.Id < 0) || ((DataList1.Items.Count() -1) < item.Id)) return; // assume item.id is index and integer value 

var dlItem = DataList1.Items[item.Id]; 
if(item !=null){ 
    rdb1 = dlItem.FindControl("One") as RadioButton; 
    rdb2 = dlItem.FindControl("Three") as RadioButton; 
    rdb3 = dlItem.FindControl("Seven") as RadioButton; 
} 
0

ID가 아닌 색인별로 항목에 액세스하는 것이 좋습니다.

당신은 그리드보기에서 페이징을 사용하도록 설정 한 경우이 오류가 발생

RadioButton rdb2 = new RadioButton(); 
RadioButton rdb3 = new RadioButton(); 
rdb1 = (RadioButton)DataList1.Items[CurrentIndex].FindControl("One"); 
rdb2 = (RadioButton)DataList1.Items[CurrentIndex].FindControl("Three"); 
rdb3 = (RadioButton)DataList1.Items[CurrentIndex].FindControl("Seven"); 
0

있다. 표에서 레코드를 삭제하려면 다음과 같이해야합니다.

int index = Convert.ToInt32 (e.CommandArgument);
int i = 인덱스 % 20;
// 여기 20이 GridView의 페이지 크기입니다.
GridViewRow row = gvMainGrid.Rows [i];
int id = Convert.ToInt32 (gvMainGrid.DataKeys [i] .Value);
새로운 GetData(). DeleteRecord (id);
GridView1.DataSource = RefreshGrid();
GridView1.DataBind();

호프이 질문에 대한 답변입니다.

관련 문제

 관련 문제