代码:
private  void Page_Load(object sender, EventArgs e)
    {
        SqlConnection MyConnection;
        String ConnStr, strCon;
        newsid = Request.Params["ID"];
        MyConnection = new SqlConnection("Initial Catalog=sol;Data Source=hnu-a4cafe281e0\\sqlexpress;Integrated Security=SSPI;");
        MyConnection.Open();
        
        //String
        strCon = "SELECT Title ,Content,ComeTo,Counter ,Time FROM TableNews1 where ID='" + newsid + "'";
        ds = new DataSet();        SqlDataAdapter myCommand = new SqlDataAdapter(strCon, MyConnection); 
        myCommand.Fill(ds, "TableNews1"); 
        if (ds.Tables["TAbleNews1"].Rows.Count > 0)
        {
            Session["ID"] = ds.Tables["TableNews1"].Rows[0];
        }
        strCon = "SELECT Counter FROM TableNews1 WHERE ID = '" + newsid + "'";      
      // dr = ds.Tables["TableNews1"].Rows[0];
        
      SqlCommand myCommand2 = new SqlCommand(strCon, MyConnection);
      SqlDataReader reader = myCommand2.ExecuteReader();
      reader.Read();          int i = reader.GetInt32(0);
          i++;
      
        reader.Close();
        strCon = "UPDATE TableNews1 SET Counter = " + i.ToString() + " WHERE ( ID= " + newsid + ")";
        myCommand2.CommandText = strCon;
        myCommand2.ExecuteNonQuery();
        MyConnection.Close();  
    } 
    }报错:在没有任何数据时进行无效的读取尝试。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。源错误: 
行 51:       reader.Read();
行 52: 
行 53:           int i = reader.GetInt32(0);
行 54:           i++;
行 55:       
 

解决方案 »

  1.   

    编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0103: 当前上下文中不存在名称“i”源错误: 
            SqlCommand myCommand2 = new SqlCommand(strCon, MyConnection);
            SqlDataReader reader = myCommand2.ExecuteReader();        if (reader.Read())
            {            int i = reader.GetInt32(0);
                i++;        }
            reader.Close();
            strCon = "UPDATE TableNews1 SET Counter = " + i.ToString() + " WHERE ( ID= " + newsid + ")";        myCommand2.CommandText = strCon;
            myCommand2.ExecuteNonQuery();
            MyConnection.Close();
        }}
    改了以后报错:行 61: 
    行 62: 
    行 63:         strCon = "UPDATE TableNews1 SET Counter = " + i.ToString() + " WHERE ( ID= " + newsid + ")";
    行 64: 
    行 65:         myCommand2.CommandText = strCon;