注册 登录
编程论坛 ASP.NET技术论坛

"System.IndexOutOfRangeException: 在位置 0 处没有任何行"错误!怎么改啊

apple0129 发布于 2008-04-10 19:57, 4460 次点击
我的数据库中有数据但总出现dr = ds.Tables["entermx"].Rows[0];一行有错,不知怎么修改。我是从一个页面的DataGrid中一个链接列实现的一个链接页
public class entergoodsmx : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        public String entergoodsid;
        public DataRow dr;
private void Page_Load(object sender, System.EventArgs e)
        {
            //取得上个页面传递来的参数值
            entergoodsid = Request.Params["entergoodsid"];
            
            SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=;database=peisong");
            //sqlcon.Open;
            string entermx="select * from entermx where entergoodsid =" + Convert.ToInt32(entergoodsid);
            SqlDataAdapter MyCommandnews = new SqlDataAdapter(entermx,sqlcon);
            DataSet ds = new DataSet ();
            MyCommandnews.Fill(ds,"entermx");
            dr = ds.Tables["entermx"].Rows[0];
}
}
2 回复
#2
apple01292008-04-10 20:08
我这里涉及到两个表,entergoodsd(主键是entergoodsid)和entermx(外键是entergoodsid)
#3
hebingbing2008-04-10 21:00
先判断一下是不是真的有数据……
int ct=ds.table["entermx"].rows.count
if(ct>0)
{
 dr = ds.Tables["entermx"].Rows[0];
}
else
{
response.write("没有数据……")
}
这样看看到底有没有数据,要是没有数据再看看你的前面的gridview带的参数是怎么样的,要不直接用response.write输出entergoodsid看看到底是什么东西……
很有可能就是entergoodsid的问题……
1