public DataRow dr;
public string newsid;
private void Page_Load(object sender, System.EventArgs e)
{
//创建数据库连接,获取新闻信息
newsid=Request.Params["id"];
string strConn =ConfigurationSettings.AppSettings["dsn"];
SqlConnection myConnection = new SqlConnection(strConn);
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM news WHERE id='"+newsid+"'", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "news");
dr = ds.Tables["news"].Rows[0];
//获取新闻的点击率
SqlCommand myCommand2= new SqlCommand("select click from news where id='"+newsid+"'",myConnection);
myCommand2.Connection.Open();
SqlDataReader reader = myCommand2.ExecuteReader();
reader.Read();
int i = reader.GetInt32(0);
i = i + 1;
reader.Close();
//更新新闻的点击率
myCommand2.CommandText = "UPDATE news SET click = "+i.ToString()+" WHERE id= '"+newsid+"'";
myCommand2.ExecuteNonQuery();
myCommand2.Connection.Close();
myConnection.Close();
}
public string newsid;
private void Page_Load(object sender, System.EventArgs e)
{
//创建数据库连接,获取新闻信息
newsid=Request.Params["id"];
string strConn =ConfigurationSettings.AppSettings["dsn"];
SqlConnection myConnection = new SqlConnection(strConn);
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM news WHERE id='"+newsid+"'", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "news");
dr = ds.Tables["news"].Rows[0];
//获取新闻的点击率
SqlCommand myCommand2= new SqlCommand("select click from news where id='"+newsid+"'",myConnection);
myCommand2.Connection.Open();
SqlDataReader reader = myCommand2.ExecuteReader();
reader.Read();
int i = reader.GetInt32(0);
i = i + 1;
reader.Close();
//更新新闻的点击率
myCommand2.CommandText = "UPDATE news SET click = "+i.ToString()+" WHERE id= '"+newsid+"'";
myCommand2.ExecuteNonQuery();
myCommand2.Connection.Close();
myConnection.Close();
}
--------------------------------------------------------------------------------在位置 0 处没有任何行。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 在位置 0 处没有任何行。源错误:
行 29: DataSet ds = new DataSet();
行 30: myCommand.Fill(ds, "news");
行 31: dr = ds.Tables["news"].Rows[0];
行 32: //获取新闻的点击率
行 33: SqlCommand myCommand2= new SqlCommand("select click from news where id='"+newsid+"'",myConnection);
源文件: d:\inetpub\wwwroot\shenjiweb\newsdetail.aspx.cs 行: 31 堆栈跟踪:
[IndexOutOfRangeException: 在位置 0 处没有任何行。]
System.Data.DataRowCollection.get_Item(Int32 index)
shenjiweb.newsdetail.Page_Load(Object sender, EventArgs e) in d:\inetpub\wwwroot\shenjiweb\newsdetail.aspx.cs:31
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
string strConn =ConfigurationSettings.AppSettings["dsn"];
SqlConnection myConnection = new SqlConnection(strConn);
//加多一行
myConnention.Open();
//执行完以后记得用Close();
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM news WHERE id='"+newsid+"'", myConnection); 向命运发出战书,誓将命运踩在脚下!!
DataSet ds = new DataSet();
myCommand.Fill(ds, "news");
此newsid不存在,所以找不到这条记录
http://community.csdn.net/Expert/topic/4333/4333138.xml?temp=.7571985