索引超出了数组界限!——在线急等!!!
sql1 = "select mymoney from item_1 where name like '%" + item_name.Text + "%'and mydate>='" + year1 + "' and mydate<='" + year2 + "'order by mydate ";
DataTable dt = DB.GetDataTable(sql1);
for (int i = Convert.ToInt32(year1); i < Convert.ToInt32(year2); i++)
{
name1 += i.ToString() + "\t";
}
name1 += year2;
string[] m = new string[dt.Rows.Count];
for (int p = 0; p < dt.Rows.Count; p++)
{
m[p] = dt.Rows[p]["mymoney"].ToString();
}
for (int p = 0; p < dt.Rows.Count - 1; p++)
{
name2 += m[p] + "\t";
}
name2 += m[dt.Rows.Count - 1];//此处出现索引超出了数组界限
sCategorys = name1;
sValues = name2;
name1 = "";
name2 = "";
------解决方案--------------------dt.Rows.Count 如果返回是0行
m[dt.Rows.Count - 1] 这成了 m[-1] 就出界了
------解决方案--------------------加个判断
if (dt.Rows.Count > 0)
{
// 处理
}
else
{
// 这可处理,也可不要 else { }
}