2014-11-03 3 views
-4

"인덱스가 배열의 경계를 벗어났습니다"예외가 계속 발생합니다. 나는 파일 이름을 가진 datatable을 가지고있다. 웹 양식 페이지에는 태블릿에 연결된 목록 상자가 있습니다. 목록 상자에서 여러 옵션을 선택하려고합니다. 그런 다음 각 파일 이름을 처리기 .ashx 파일로 보냅니다. 내가 볼 수있는 for 루프를 사용하고있다. 여기 "인덱스가 배열의 경계를 벗어났습니다"라는 예외를 수정하는 방법은 무엇입니까?

는 main.aspx.cs 코드 :

namespace MultiImagesImageHandler 
{ 
public partial class Main : System.Web.UI.Page 
{ 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     int[] AllselectedIndex = ListBox1.GetSelectedIndices(); 
     int a = AllselectedIndex.Length; 

     DataView tempDV = SqlDataSource1.Select(DataSourceSelectArguments.Empty) as DataView; 

     for (int i = 0; i < a + 1; i++) 
     { 
      string fn = tempDV.Table.Rows[AllselectedIndex[i+1]].ItemArray[1].ToString(); 
      ImgHandler.filename[i] = fn.Trim(); 
     } 

     Response.Redirect("image1.htm"); 

    } 
} 
} 

handler.ashx.cs 코드 :

namespace MultiImagesImageHandler 
{ 
/// <summary> 
/// Summary description for ImgHandler 
/// </summary> 
public class ImgHandler : IHttpHandler 
{ 
    public static string[] filename = new string[16]; 


    public void ProcessRequest(HttpContext context) 
    { 
     string saveTo = @"fileLocation" + filename[3]; 
     FileStream writeStream1 = new FileStream(saveTo, FileMode.OpenOrCreate, FileAccess.ReadWrite); 
     string loadFrom = @"fileLocation" + filename[3]; 
     using (FileStream fs1 = File.Open(loadFrom, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) 
     { 
      ReadWriteStream(fs1, writeStream1); 
     } 
     byte[] tt = File.ReadAllBytes(context.Server.MapPath("~/images/" + filename[3])); 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.BinaryWrite(tt); 

    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

    // readStream is the stream you need to read 
    // writeStream is the stream you want to write to 
    private void ReadWriteStream(Stream readStream, Stream writeStream) 
    { 
     int Length = 256; 
     Byte[] buffer = new Byte[Length]; 
     int bytesRead = readStream.Read(buffer, 0, Length); 
     // write the required bytes 
     while (bytesRead > 0) 
     { 
      writeStream.Write(buffer, 0, bytesRead); 
      bytesRead = readStream.Read(buffer, 0, Length); 
     } 
     readStream.Close(); 
     writeStream.Close(); 
    } 
} 
} 

나는이와 ImgHandler.ashx.cs의 배열을 채울 것이라고 생각 선택한 파일 이름은 데이터 테이블에서 가져 왔지만 실제로는 그렇지 않습니다. 이 예외는 계속 발생합니다. 왜 이것이 작동하지 않는지 나는 모른다.

답변

2

내 생각이다 :

for (int i = 0; i < a + 1; i++) 
{ 
    string fn = tempDV.Table.Rows[AllselectedIndex[i+1]].ItemArray[1].ToString(); 
    ImgHandler.filename[i] = fn.Trim(); 
} 

변경은 "A + 1"그냥 "A"

+0

나는 바보처럼 느껴진다. 또한 AllselectedIndex [I + 1]을 i로 변경해야했습니다. – user3634308

+0

@ user3634308 아 하, 잘 잡으세요! – Kaz

3

당신은이 :

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

가해야합니다이 문제

for (int i = 0; i < a; i++) 
0
string sRecontent = txtrepalcecontent.Text; 
    string[] fileLineString = new string[sContent.Length]; 
    for (int i =0 ; i < sContent.Length; i++) 

    { 
     //fileLineString[0] = sContent[0].ToString(); 
     string[] content = sContent.Split(delim); 
     if (sFind == content[i]) **//index was outside the bounds the array** 
     { 
      string A = sContent.Remove(i, txtfind.Text.Length); 
      string S = A.Insert(i, sReplace); 
      txtrepalcecontent.Text = S; 
     } 
+0

답변에 세부 사항을 추가하는 것을 잊지 마십시오. 잘못되었거나 변경중인 정보를 추가하십시오. –

관련 문제