当前位置: 代码迷 >> ASP.NET >> asp.net请问
  详细解决方案

asp.net请问

热度:9794   发布时间:2013-02-25 00:00:00.0
asp.net请教
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class User_BulletinInfo : System.Web.UI.Page
{
  string strSql;
  WebService webService = new WebService();
  DataTable dtTable = new DataTable();
  protected void Page_Load(object sender, EventArgs e)
  {
 
  string ID=string.IsNullOrEmpty(Request.QueryString["bid"])?"":Request.QueryString["bid"];
  strSql = "Update BulletinInfo set Bul_point=Bul_point+1 where Bul_ID='" + ID + "';";


  // strSql = "Update BulletinInfo set Bul_point=Bul_point+1 where Bul_ID='" + Request.QueryString["bid"].ToString() + "';";
  webService.ExcuteSelect(strSql);
  string ID1 = string.IsNullOrEmpty(Request.QueryString["bid"]) ? "" : Request.QueryString["bid"];
  strSql = "Select Bul_title,Bul_content,Bul_point from BulletinInfo where Bul_ID='" + ID1 + "';";

  // strSql = "Select Bul_title,Bul_content,Bul_point from BulletinInfo where Bul_ID='" + Request.QueryString["bid"].ToString() + "';";
  dtTable = webService.ExcuteSelect(strSql);
  this.lblBulTitle.Text = dtTable.Rows[0]["Bul_title"].ToString();
  this.lblBulContent.Text = dtTable.Rows[0]["Bul_content"].ToString();
  this.lblPoint.Text = dtTable.Rows[0]["Bul_point"].ToString();

  }
}
错误指向:this.lblBulTitle.Text = dtTable.Rows[0]["Bul_title"].ToString();
错误说明是:用户代码未处理IndexOutofRangeException
在位置 0 处没有任何行

------解决方案--------------------------------------------------------
这个有可能是webService.ExcuteSelect(strSql)执行后的结果集中没有记录导致的。因为dtTable里没有数据,自然也就不存在dtTable.Rows[0]这个元素,所以继续访问dtTable.Rows[0]["Bul_content"]自然就会报错。建议你单步调试一下,看看执行完成之后DataTable.Rows.Count的值是多少;如果有可能为0,那么建议把最后4行做如下修改:
C# code
dtTable = webService.ExcuteSelect(strSql); if(dtTable.Rows.Count > 0){    this.lblBulTitle.Text = dtTable.Rows[0]["Bul_title"].ToString();     this.lblBulContent.Text = dtTable.Rows[0]["Bul_content"].ToString();     this.lblPoint.Text = dtTable.Rows[0]["Bul_point"].ToString(); }
------解决方案--------------------------------------------------------
dtTable里面没有数据!!先if(dtTable.Rows.Count > 0)判断一下
------解决方案--------------------------------------------------------
把你那个strSql放在SQL SERVER查询分析器里运行看下.
  相关解决方案