异常详细信息: 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查询语句,可以查出结果,但是还是运行失败,请各位高手指点一下
行 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查询语句,可以查出结果,但是还是运行失败,请各位高手指点一下
解决方案 »
- winform中如何读取普通文件按exe执行呢
- 各位IT前辈给指点一下,小子不胜感激啊
- VS使用断点,调试多线程程序时,不执行代码!原来好好的,添加单元测试后不能调试了。
- 100分问WINDOWS窗体登陆界面问题。。。
- 最近学C#困惑?
- c/s架构客户端和主控端的数据传输
- c# ReportDocument 类执行方法PrintToPrinter()方法时报错
- 如何用remoting实现远程文件的操作!急急急!
- Web应用中,写一个link,然后让用户点击这个link下载服务端的文件,link内容怎么写
- 新人,求大神帮帮忙
- 请问如何实现Datetime类型的日期相加。
- 向一个数组添加循环的值的问题
this.classfied.Text = ds.Tables["Category"].Rows[0]["name"].ToString();
this.lbcontent.Text = ds.Tables["Category"].Rows[0]["content"].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字段是存放点击次数的
string sql;
sql = "SELECT clickcount=clickcount+1 from 你的字段";
//再或者
sql = "SELECT MAX(clickcount)+1 from 你的字段";
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楼的改一改就更实用了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";
}
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");
sqlda.Fill(dt);
if(dt!=null && dt.rows.count>0)
{
this.newstitle.Text = dt.Rows[0]["title"].ToString();
//.....
}