第一个报错:在位置 0 处没有任何行。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 在位置 0 处没有任何行。源错误: 
行 41:  DataSet ds = new DataSet();
行 42:  myCommand.Fill(ds,"news");
行 43:  dr = ds.Tables["news"].Rows[0];
行 44:  //显示新闻信息
行 45:  title.Text=dr["title"].ToString();第二个报错:索引超出范围。必须为非负值并小于集合大小。参数名: index 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index源错误: 
行 175: SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
行 176: myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Char, 11));
行 177: myCommand.Parameters["@id"].Value =DataGrid1.DataKeys[(int)(e.Item.ItemIndex)];
行 178:
行 179: myCommand.Connection.Open();
 
第三个报错:输入字符串的格式不正确。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误: 
行 129: btnLast.Enabled=true;
行 130: string arg=e.CommandArgument.ToString();
行 131: PageCount=Int32.Parse(lblPageCount.Text.ToString());
行 132: int pageindex=Int32.Parse(lblCurrentPage.Text.ToString())-1;
行 133: switch(arg)
 
 

解决方案 »

  1.   

    第一个显然是查找不到记录。
    第二个
    myCommand.Parameters["@id"].Value =DataGrid1.DataKeys[(int)(e.Item.ItemIndex)];
    应该ItemIndex值大于DataKeys.Count;
    第三个
    PageCount=Int32.Parse(lblPageCount.Text.ToString());
    int pageindex=Int32.Parse(lblCurrentPage.Text.ToString())-1;
    应该其中有个text不是数字的字符。这东西楼主调试一下就可以了
      

  2.   

    第一个报错:在位置 0 处没有任何行。
    -----------------------------
    你的DataTable里没有数据,你取 ds.Tables["news"].Rows[0] 当然报错了第二个报错:索引超出范围。必须为非负值并小于集合大小。参数名: index 
    -----------------------------------------------------------
    错误在这里:
    myCommand.Parameters["@id"].Value =DataGrid1.DataKeys[(int)(e.Item.ItemIndex)];
    e.Item.ItemIndex 的数值大于 DataGrid1.DataKeys.Count 了
    第三个报错:输入字符串的格式不正确。 
    ----------------------------------------
    lblPageCount.Text 或者 lblCurrentPage.Text 里存的不是数字类型,猜想可能为空字符串
      

  3.   

    *?你的DataTable里没有数据,你取 ds.Tables["news"].Rows[0] 当然报错了?
    SqlConnection myConnection = new SqlConnection(strConn);
    SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM news WHERE id='"+newsid+"'",myConnection); 
    DataSet ds = new DataSet();
    myCommand.Fill(ds,"news");
    我明明连了数据库呀,表里也有东东的呀
      

  4.   

    你传的id对吗,这个问题你加个断点调试一下就可以了,看看生成的sql对不对就行了。
      

  5.   

    (蓝色闪电)你回在来吗?lblPageCount.Text 是空的,我显示不出来,不知道我该怎么改?
      

  6.   

    看看这个SELECT * FROM news WHERE id='"+newsid+"'有数据吗?
      

  7.   

    在别的地方我也用了这个SELECT * FROM news WHERE id='"+newsid+"',没报错,我怀疑可能不是它的问题谢谢Tray
      

  8.   

    多谢楼主的信任,刚刚在忙的东西。对于第一个问题:在位置 0 处没有任何行。
    --------------------------
    首先你要确定你的查询语句能查出东西来,你要重点检查你myCommand里CommandText存的查询语句是否有问题(特别是newsid里存的东西,很可能你的数据库里没有关于newsid的数据)。对于第二个问题:索引超出范围。必须为非负值并小于集合大小。参数名: index
    -------------------------- 
    你重点检查 e.Item.ItemIndex 和 DataGrid1.DataKeys.Count ,看前者是否大于后者对于第二个问题:输入字符串的格式不正确
    -----------------------------
    可以这样改:System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^\d+$");PageCount = (reg.Match(lblPageCount.Text).Success ? Convert.ToInt32(lblPageCount.Text) : 0);int pageindex = (reg.Match(lblCurrentPage.Text).Success ? Convert.ToInt32(lblCurrentPage.Text) : 0);