public string[] pic = new string[5];
public string[] link = new string[5];
protected System.Web.UI.HtmlControls.HtmlGenericControl inde;
public string[] min=new string[5];
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection con = new SqlConnection("server=.;database=Systen;user id=sa;password=;");
string str_top="select top 5 category from product_category_table where grade=1 and changes=0 order by bys";
SqlDataAdapter da = new SqlDataAdapter(str_top, con);
StringBuilder sb_slist=new StringBuilder();
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
{
string str_Temp_Pic = "";
string str_Temp_Link = "";
string str_info = "select pr_image,pro_url from product_image where pro_type='" + ds.Tables[0].Rows[m][0].ToString() + "'";
SqlDataAdapter da1 = new SqlDataAdapter(str_info, con)
sb_slist.Append("<td align=\"center\" width=\"160\">");
sb_slist.Append(ds.Tables[0].Rows[m][0].ToString());
sb_slist.Append("<td>");
DataSet ds1 = new DataSet();
da1.Fill(ds1);
if (ds1.Tables[0].Rows.Count > 0)
{
for (int n = 0; n < ds1.Tables[0].Rows.Count; n++)
{
str_Temp_Pic+="webupload/"+ds1.Tables[0].Rows[n][0].ToString() + "|";
str_Temp_Link += ds1.Tables[0].Rows[n][1].ToString() + "|";
//str_Temp_cname += ds.Tables[0].Rows[m][0].ToString() + "|";
}
}
pic[m] = str_Temp_Pic.Substring(0,str_Temp_Pic.Length - 1);
link[m] = str_Temp_Link.Substring(0, str_Temp_Link.Length - 1); 
}
}
this.inde.InnerHtml=sb_slist.ToString();
}
为什么会报一个长度不能小于 0。参数名: length 

解决方案 »

  1.   

    跟踪一下,看看str_Temp_Pic和str_Temp_Link是否是""
      

  2.   


    SqlConnection con = new SqlConnection("server=.;database=Systen;user id=sa;password=;"); 
            con.Open();
    string str_top="select top 5 category from product_category_table where grade=1 and changes=0 order by bys"; 
    SqlDataAdapter da = new SqlDataAdapter(str_top, con); 
    StringBuilder sb_slist=new StringBuilder(); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    if (ds.Tables[0].Rows.Count > 0) 

    for (int m = 0; m < ds.Tables[0].Rows.Count; m++) 

    string str_Temp_Pic = ""; 
    string str_Temp_Link = ""; 
    string str_info = "select pr_image,pro_url from product_image where pro_type='" + ds.Tables[0].Rows[m][0].ToString() + "'"; 
    SqlDataAdapter da1 = new SqlDataAdapter(str_info, con) 
    sb_slist.Append(" <td align=\"center\" width=\"160\">"); 
    sb_slist.Append(ds.Tables[0].Rows[m][0].ToString()); 
    sb_slist.Append(" <td>"); 
    DataSet ds1 = new DataSet(); 
    da1.Fill(ds1); 
    if (ds1.Tables[0].Rows.Count > 0) 

    for (int n = 0; n < ds1.Tables[0].Rows.Count; n++) 

    str_Temp_Pic+="webupload/"+ds1.Tables[0].Rows[n][0].ToString() + " ¦"; 
    str_Temp_Link += ds1.Tables[0].Rows[n][1].ToString() + " ¦"; 
    //str_Temp_cname += ds.Tables[0].Rows[m][0].ToString() + " ¦"; 


    pic[m] = str_Temp_Pic.Substring(0,str_Temp_Pic.Length - 1); 
    link[m] = str_Temp_Link.Substring(0, str_Temp_Link.Length - 1); 


    this.inde.InnerHtml=sb_slist.ToString(); 
    }
      

  3.   

    str_Temp_Pic.Length或者str_Temp_Link.Length是0吧,要不然也不会报这个错误。
    建议加个判断,
    pic[m] = str_Temp_Pic.Length>0?str_Temp_Pic.Substring(0,str_Temp_Pic.Length - 1):""; 
    link[m] = str_Temp_Link.Length>0?str_Temp_Link.Substring(0, str_Temp_Link.Length - 1):""; 
      

  4.   

    Substring时建议加一个判断if(str_Temp_Pic.Length>0)