标题:数组访问越界
只看楼主
fanjinxiang
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2012-10-15
结帖率:100%
已结贴  问题点数:20 回复次数:5 
数组访问越界
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=fjx;User ID=sa;Password=1234");
            SqlCommand com = conn.CreateCommand();
             = "select * from Department";
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = com;
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            conn.Close();
            int i = ds.Tables[0].Rows.Count;
            if (i>0)
            {
                int iRow = ds.Tables[0].Rows.Count;
                int iColumn = 2;
                for (int j = 0; j < iRow; j++)
                {
                    TableRow newRow = new TableRow();
                    tblshow.Controls.Add(newRow);
                    for (int k = 0; k < iColumn; k++)
                    {
                        TableCell newCell = new TableCell();
                        string strContent = "";
                        if (k == 0)
                        {
                            int iIndex = j + 1;
                            strContent = iIndex.ToString();
                        }
                        else
                        {
                            strContent = ds.Tables[0].Rows[j][k].ToString();
                        }
                        newCell.Text = strContent;
                        newRow.Controls.Add(newCell);
                    }
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~\\Default.aspx/");
    }
}运行后提示我数组访问越界,什么问题
搜索更多相关主题的帖子: 越界 void public 
2012-11-04 21:03
naska
Rank: 1
等 级:新手上路
帖 子:4
专家分:3
注 册:2012-11-4
得分:3 
这个表有多少列啊?程序中固定为2,是否是这个原因?加些调试信息看看
2012-11-04 22:09
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3450
专家分:19340
注 册:2012-3-31
得分:3 
看看

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-11-05 22:49
跳过去
Rank: 8Rank: 8
等 级:贵宾
威 望:20
帖 子:282
专家分:976
注 册:2012-8-13
得分:3 
超出了索引

光棍中.....
2012-11-05 23:07
zhangjian123
Rank: 2
等 级:论坛游民
威 望:1
帖 子:13
专家分:41
注 册:2012-10-29
得分:3 
跟下代码 就看到了的啊
2012-11-06 09:39
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
得分:3 
检查一下这句代码   int i = ds.Tables[0].Rows.Count;
看是否成功从数据库拿到数据,如果没有成功拿到数据,ds.Tables.Count=0,你用ds.Tables[0],当然会产生越界。

欢迎加入.Net技术交流群:90925122
2012-11-06 13:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-385911-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.110192 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved