protected void btCheck_Click(object sender, EventArgs e)
    {
        //获取参数
        string checkResult = this.txtCheck.Text;
        string[] str = new string[20];
        str = checkResult.Split(',');
        string PortCode1 = str[0];
        string PortCode2 = str[1];
        string PortCode3 = str[2];
        string PortCode4 = str[3];
        string PortCode5 = str[4];
        string PortCode6 = str[5];
        string PortCode7 = str[6];
        string PortCode8 = str[7];
        string PortCode9 = str[8];
        string PortCode10 = str[9];
        string PortCode11 = str[10];
        string PortCode12 = str[11];
        string PortCode13 = str[12];
        string PortCode14 = str[13];
        string PortCode15 = str[14];
        string PortCode16 = str[15];
        string PortCode17 = str[16];
        string PortCode18 = str[17];
        string PortCode19 = str[18];
        string PortCode20 = str[19];
        string ResourceId = Request.QueryString["ResourceId"];
        this.div1.Style.Add("display", "none");//隐藏
        this.div2.Style.Add("display", "");//显示
        DataSet ds = new DataSet();
        ds = new ResourcePort().GetResourcePort(PortCode1, PortCode2, PortCode3, PortCode4, PortCode5, PortCode6, PortCode7, PortCode8, PortCode9, PortCode10, PortCode11, PortCode12, PortCode13, PortCode14, PortCode15, PortCode16, PortCode17, PortCode18, PortCode19, PortCode20, ResourceId);
       int inqw = ds.Tables[0].Rows.Count;
       
       this.DataList1.DataSource = ds;
       this.DataList1.DataBind();    }如上代码....会报数组下标越界...问问各位,应该怎样解决呢?
可以把str 这个数组里面的一个一个的取出来直接赋值给 这些参数吗?如果没有就为nullPortCode1-20,这些可以为null也可以有值的,这样看checkResult传过来的是什么的,如checkResult = "123",那么 PortCode1 = "123",其他PortCode2-120就为null;如checkResult ="123,321,2",那么PortCode1 = "123",PortCode2 = "321", PortCode3 ="2",PortCode4-20也为null。就是说要根据checkResult来获得数据的...谁知道说说,思路也行..先谢谢了...

解决方案 »

  1.   

    你的代码 有越界 可能性存在!
    你并没理解好 Split 的 意义!string[] str = new string[20];
            str = checkResult.Split(',');
    如果 checkResult="1111,333,4444,555,76767";str.Length 值为 5你写到str[6] 就越界了! 明白了嘛
      

  2.   

     str = checkResult.Split(',');
    这是语句执行后的数组大小是不定的 checkResult.Split(',')中有多小个就是多少个使用前应该判断
      

  3.   


    protected void btCheck_Click(object sender, EventArgs e)
        {
            //获取参数
            string checkResult = this.txtCheck.Text;
            string[] str =checkResult.Split(',');
            string PortCode1 = null;
    if(str.Length>0) 
     PortCode1 = str[0];
    if(str.Length>1) 
     PortCode2 = str[1];
    if(str.Length>2) 
     PortCode3 = str[2];
    if(str.Length>3) 
     PortCode4 = str[3];
    .....
      

  4.   

    你先判断 checkResult.Split(','); 出来了多少个逗号  然后 再给 str 数字分配 count。
      

  5.   


           string checkResult = this.txtCheck.Text;
            string[] PortCodes= checkResult.Split(',');
           string ResourceId = Request.QueryString["ResourceId"];
            this.div1.Style.Add("display", "none");//隐藏
            this.div2.Style.Add("display", "");//显示
            DataSet ds = new DataSet();
            ds = new ResourcePort().GetResourcePort(PortCodes, ResourceId);
           int inqw = ds.Tables[0].Rows.Count;
           
           this.DataList1.DataSource = ds;
           this.DataList1.DataBind();
      

  6.   

    先判断了str.Length,再往下写代码就行了
      

  7.   

      string checkResult = this.txtCheck.Text;
    看看他里面的数据对不!
    str[0];写成str[0].ToString();
      

  8.   


                string a = "12,34,55,684";
                string[] arr = a.Split(',');
                List<string> lists = new List<string>();
                for (int i = 0; i <20; i++)
                {
                    if (i < arr.Length)
                    {
                        lists.Add(arr[i]);
                    }
                    else
                    {
                        lists.Add(null);  //这里的lists[0]---lists[19]相当于你的PortCode1到PortCode20
                    }
                }
                for (int i = 0; i < 20; i++)
                {
                    if(lists[i]!=null)
                    Console.WriteLine(lists[i]); 
                }
                Console.ReadLine();
      

  9.   

            string[] str = new string[20];//起先是20个元素
            str = checkResult.Split(',');//到这里就变成5个元素了
      

  10.   


    刚刚用了LS朋友的方法,是对的,但是我在后台查询出来的数据在页面显示的时候不对..希望大家看看下面的代码,看有什么不对的..前台的我也贴上..前代代码:
     protected void btCheck_Click(object sender, EventArgs e)
        {
            //获取参数
            string checkResult = this.txtCheck.Text;        string[] strs = checkResult.Split(',');        string[] PortCode = new string[20];        string[] arr = checkResult.Split(',');        List<string> lists = new List<string>();        for (int i = 0; i < 20; i++)
            {
                if (i < arr.Length)
                {
                    lists.Add(arr[i]);
                }
                else
                {
                    lists.Add(null); 
                }
            }        for (int i = 0; i < 20; i++)
            {
                PortCode[i] = lists[i];
            }       string ResourceId = Request.QueryString["ResourceId"];
           this.div1.Style.Add("display", "none");//隐藏
            this.div2.Style.Add("display", "");//显示
            DataSet ds = new DataSet();        ds = new ResourcePort().GetResourcePort(PortCode, ResourceId);
            int inqw = ds.Tables[0].Rows.Count;        this.DataList1.DataSource = ds;
            this.DataList1.DataBind();    }后台代码: public DataSet GetResourcePort(string[] PortCode, string ResourceId)
            {
                StringBuilder strSql = new StringBuilder();            strSql.Append(@"select * from ResourcePort where portcode2 = @PortCode20
                                     or portcode2 = @PortCode1 or portcode2 = @PortCode2
                                     or portcode2 = @PortCode3 or portcode2 = @PortCode4
                                     or portcode2 = @PortCode5 or portcode2 = @PortCode6
                                     or portcode2 = @PortCode7 or portcode2 = @PortCode8
                                     or portcode2 = @PortCode9 or portcode2 = @PortCode10
                                     or portcode2 = @PortCode11 or portcode2 = @PortCode12
                                     or portcode2 = @PortCode13 or portcode2 = @PortCode14
                                     or portcode2 = @PortCode15 or portcode2 = @PortCode16
                                     or portcode2 = @PortCode17 or portcode2 = @PortCode18
                                     or portcode2 = @PortCode19  and  resourceid = @ResourceId ");            SqlParameter[] paramenters = { new SqlParameter("@PortCode1", PortCode[0]),
                                                 new SqlParameter("@PortCode2", PortCode[1]),
                                                 new SqlParameter("@PortCode3", PortCode[2]),
                                                 new SqlParameter("@PortCode4", PortCode[3]),
                                                 new SqlParameter("@PortCode5", PortCode[4]),
                                                 new SqlParameter("@PortCode6", PortCode[5]),
                                                 new SqlParameter("@PortCode7", PortCode[6]),
                                                 new SqlParameter("@PortCode8", PortCode[7]),
                                                 new SqlParameter("@PortCode9", PortCode[8]),
                                                 new SqlParameter("@PortCode10", PortCode[9]),
                                                 new SqlParameter("@PortCode11", PortCode[10]),
                                                 new SqlParameter("@PortCode12", PortCode[11]),
                                                 new SqlParameter("@PortCode13", PortCode[12]),
                                                 new SqlParameter("@PortCode14", PortCode[13]),
                                                 new SqlParameter("@PortCode15", PortCode[14]),
                                                 new SqlParameter("@PortCode16", PortCode[15]),
                                                 new SqlParameter("@PortCode17", PortCode[16]),
                                                 new SqlParameter("@PortCode18", PortCode[17]),
                                                 new SqlParameter("@PortCode19", PortCode[18]),
                                                 new SqlParameter("@PortCode20", PortCode[19]),
                                              new SqlParameter("@ResourceId", ResourceId),};
                return DbHelperSQL.Query(strSql.ToString(), paramenters);
            }
      

  11.   

       用楼的代码  思路是这样的
       首先是截取字符串   将截取的字符串用 list 保存,在声明一个数组 PortCode,在用for循环把结果都循环赋值给 PortCode数组,在把这个数组传给这个查询的方法。
       后台的查询方法传2个参数,一个 数组,一个 id,再用sql语句查询,sql参数的值是用PortCode下标来确定的..但是查询出来的数据在页面上显示是错误的,多出了很多的垃圾信息,但是我在 sql  server2005中运行的时候,是可以查询出数据的,而且是对的...  大家帮我看看把....谢谢啦》。