异常详细信息: System.IndexOutOfRangeException: 在位置 0 处没有任何行。源错误: 
行 38:         sqlda.Fill(ds, "Category");//由adapter对象把category表填充到dataset中
行 39: 
行 40:         this.newstitle.Text = ds.Tables[0].Rows[0]["title"].ToString();
行 41:         this.classfied.Text = ds.Tables[0].Rows[0]["name"].ToString();
行 42:         this.lbcontent.Text = ds.Tables[0].Rows[0]["content"].ToString();
 
源代码:
public partial class newscontent : System.Web.UI.Page
{    protected string nid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            nid = Request.QueryString["id"].ToString();
        } 
         GetDataBynewsId();
        if (!IsPostBack)
        {
            GetCountClick();
        }
    }
    protected void GetDataBynewsId()
    {
        string conStr = @"Data Source = .\SQLEXPRESS;AttachDbFilename=|DataDirectory|\newssystem4.mdf;Integrated Security=True;User Instance=True";//从配置文件中获取连接字符串
        SqlConnection sqlcon = new SqlConnection(conStr);//根据连接字符串建立数据库连接
        string sql = "SELECT category.id,category.name,news.title,news.[content],news.createTime,news.clickcount FROM news INNER JOIN category ON news.caId = category.id WHERE (news.id = '"+nid+"')";//建立sql查询语句
        sqlcon.Open();//打开数据库
        SqlDataAdapter sqlda = new SqlDataAdapter(sql, sqlcon);//dataadapter一般和dataset连用;首先建立一个适配器相当于dataset与数据库连接的桥梁,创建sql语句与连接字符串的关联
        DataSet ds = new DataSet();//新建立一个内存数据库        sqlda.Fill(ds, "Category");//由adapter对象把category表填充到dataset中        this.newstitle.Text = ds.Tables[0].Rows[0]["title"].ToString();
        this.classfied.Text = ds.Tables[0].Rows[0]["name"].ToString();
        this.lbcontent.Text = ds.Tables[0].Rows[0]["content"].ToString();
        this.newscreatetime.Text = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString()).ToString("yyyy-MM-dd");
        sqlcon.Close(); 
    }
    protected void GetCountClick()
    {
        string conStr = @"Data Source = .\SQLEXPRESS;AttachDbFilename=|DataDirectory|\newssystem4.mdf;Integrated Security=True;User Instance=True";//从配置文件中获取连接字符串
        SqlConnection sqlcon = new SqlConnection(conStr);
        sqlcon.Open();
        SqlCommand con = new SqlCommand("update news set clickcount = clickcount+1 where id = '" + nid + "'", sqlcon);
        con.ExecuteNonQuery();
        SqlDataAdapter sqlda = new SqlDataAdapter("SELECT clickcount FROM news WHERE (id = '"+nid+"')", sqlcon);
        DataSet ds = new DataSet();
        sqlda.Fill(ds, "category");
        this.countclick.Text = ds.Tables[0].Rows[0]["clickcount"].ToString();
        //return ds.Tables[0].Rows.Count == 0 ? null : product.Tables[0].Rows[0];
    }
}我检查过SQL查询语句,可以查出结果,但是还是运行失败,请各位高手指点一下

解决方案 »

  1.   

    this.newstitle.Text = ds.Tables["Category"].Rows[0]["title"].ToString(); 
            this.classfied.Text = ds.Tables["Category"].Rows[0]["name"].ToString(); 
            this.lbcontent.Text = ds.Tables["Category"].Rows[0]["content"].ToString();用填充的表名取值看看....
      

  2.   

    我把this.newstitle.Text = ds.Tables["Category"].Rows[0]["title"].ToString(); 
            this.classfied.Text = ds.Tables["Category"].Rows[0]["name"].ToString(); 
            this.lbcontent.Text = ds.Tables["Category"].Rows[0]["content"].ToString(); 
    这段代码改成 
    if (ds.Tables[0].Rows.Count != 0)
            {
                this.newstitle.Text = ds.Tables["category"].Rows[0]["title"].ToString();
                this.classfied.Text = ds.Tables["category"].Rows[0]["name"].ToString();
                this.lbcontent.Text = ds.Tables["category"].Rows[0]["content"].ToString();
                this.newscreatetime.Text = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString()).ToString("yyyy-MM-dd");
            }运行成功了,但是不能读取数据库,但是我把我的SQL代码放回 SQL SERVER进行调试,给id赋了一个值运行,可以实现与预想相同的结果,这不是说明SQL代码没有问题吗,而且里面除了clickcount字段为空之外其余的都没有NULL值,至于clickcount字段是存放点击次数的
      

  3.   

    判断完没有东东后你要插入一个默认值,然后依次加1就好了。
    string sql;
    sql = "SELECT clickcount=clickcount+1 from 你的字段";
    //再或者
    sql = "SELECT MAX(clickcount)+1 from 你的字段";
      

  4.   

    你获取一下实际运行中的SQL语句。在查询分析中看看
      

  5.   

     SqlDataAdapter sqlda = new SqlDataAdapter("SELECT clickcount FROM news WHERE id = '"+nid+"'", sqlcon); 
      DataSet ds = new DataSet(); 
      sqlda.Fill(ds);
    if(ds.Tables[0].Rows.Count>0)
     {
       this.countclick.Text = ds.Tables[0].Rows[0]["clickcount"].ToString(); 
    }
         
      

  6.   


    --------------------------------把6楼的改一改就更实用了SqlDataAdapter sqlda = new SqlDataAdapter("SELECT clickcount FROM news WHERE id = '"+nid+"'", sqlcon); 
      DataSet ds = new DataSet(); 
      sqlda.Fill(ds); 
    if(ds.Tables[0].Rows.Count>0) 

      this.countclick.Text = ds.Tables[0].Rows[0]["clickcount"].ToString(); 

    else
    {
      this.countclick.Text ="0";
    }
      

  7.   

    1、news.id 这个数据库设定的是字符串(nvarchar)的还是数值(int)。虽然没有影响结果,但是数据类型最好匹配了写,int的就不要单引号了。
    2、因为你SQL语句有关联的(FROM news INNER JOIN category ),你检查主表和副表是不是写反了,现在主表是NEWS。
     sqlda.Fill(ds, "news");//这么写看看有没有数据3、你别用DATASET,用DATATABLE,这样就不用管哪个表了,就查出来的数据就是你的SQL语句的所有数据了。
    DataTable dt = new DataTable();
    sqlda.Fill(dt);
            this.newstitle.Text = dt.Rows[0]["title"].ToString(); 
            this.classfied.Text = dt.Rows[0]["name"].ToString(); 
            this.lbcontent.Text = dt.Rows[0]["content"].ToString(); 
            this.newscreatetime.Text = DateTime.Parse(dt.Rows[0]["createTime"].ToString()).ToString("yyyy-MM-dd"); 
      

  8.   

    忘了判断了DataTable dt = new DataTable(); 
    sqlda.Fill(dt); 
    if(dt!=null && dt.rows.count>0)
    {
         this.newstitle.Text = dt.Rows[0]["title"].ToString(); 
         //.....