dept = Request.QueryString["JCid"].ToString();
sqlcmd = "select deptName from Dept where id = "+dept;
Label lb = (Label)DataList1.Items[0].FindControl("Label1");
lb.Text = service.GetValue(sqlcmd);程序这样代码上有错吗??莫非还要的html里面写代码?错误说是:“索引超出范围。必须为非负值并小于集合大小。参数名: index 
”这个怎么回事??请赐教!!!

解决方案 »

  1.   

    错误说是:“索引超出范围。必须为非负值并小于集合大小。参数名: index 
    ----------------------------------------------------------------------
    如果报出这种错误,一般都是Label lb = (Label)DataList1.Items[0].FindControl("Label1");
    中Items[0]的0有问题,你的sqlcmd = "select deptName from Dept where id = "+dept;的记录是不是只有一行,如果大于一行,肯定回出现问题的,比如有两行的话,你这里怎么只可以写0呢
    你这样写试试
    sqlcmd = "select deptName from Dept where id = "+dept;
    DataSet ds=new DataSet();
    ds=Info.Select(sqlcmd,"Dept ");
    if(ds.Table["Dept "].Rows.Count>0)
    {
    for(int j=0;j<ds.Table["Dept "].Rows.Count;j++)
    {
    Label lb = (Label)DataList1.Items[0].FindControl("Label1");
    lb.Text = service.GetValue(sqlcmd);
    }
    }
    这样写不管是0行还是多行都没有问题