asp.net 未将对象引用设置到对象的实例

[复制链接]
查看11 | 回复4 | 2019-2-12 13:27:13 | 显示全部楼层 |阅读模式
这是一个新闻管理系统,主页面单击新闻时,显示一个新闻内容对话框,
index.aspx.cs内容如下:
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;
using System.Data.SqlClient;
public partial class index : System.Web.UI.Page
{

commonclass cc = new commonclass();

protected void Page_Load(object sender, EventArgs e)

{

//时政要闻

dlsz.DataSource = cc.GetDataSet("select top 5 * from tb_news where style='时政要闻' order by id desc","tb_news");

dlsz.DataKeyField = "ID";



dlsz.DataBind();



}

protected void dlsz_ItemCommand(object source, DataListCommandEventArgs e)

{

int ID = Convert.ToInt32(dlsz.DataKeys[e.Item.ItemIndex].ToString());

Response.Write("");

}
}
shownews.aspx.cs内容如下
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;
using System.Data.SqlClient;//添加类库
public partial class showNews : System.Web.UI.Page
{

commonclass cc = new commonclass();

protected void Page_Load(object sender, EventArgs e)

{



DataSet ds = cc.GetDataSet("select * from tb_News", "tb_News");

DataRow[] row = ds.Tables["tb_News"].Select("id=" + Request.QueryString["id"]);

foreach (DataRow rs in row)

{

this.Page.Title = rs["title"].ToString();

this.labTitle.Text = rs["title"].ToString();

this.txtContent.Text =""+ rs["content"].ToString();

}
}

protected void btnClose_Click(object sender, EventArgs e)

{

Response.Write("");

}
}
请高手帮忙!
DataRow[] row = ds.Tables["tbNews"].Select("id=" + Request.QueryString["id"]);
这句出错了

回复

使用道具 举报

千问 | 2019-2-12 13:27:13 | 显示全部楼层
一般报“未将对象引用设置到对象的实例”这个错误都是有一个对象的值为空,你打个断点调试哈,这不是代码的错!...
回复

使用道具 举报

千问 | 2019-2-12 13:27:13 | 显示全部楼层
假如是新手的话代码别写简单了。多写几次。比如你这个DataRow[] row = ds.Tables["tbNews"].Select("id=" + Request.QueryString["id"]);改写成 k=ds.Tables["tbNews"]; y=Request.QueryString["id"]);然后再 k.Select( y) ...
回复

使用道具 举报

千问 | 2019-2-12 13:27:13 | 显示全部楼层
加个判断就是了if(Request.QueryString["id"]!=null&&ds.Tables["tb_News"].Rows.Count>0){
DataRow[] row = ds.Tables["tb_News"].Select("id=" + Request.QueryString["id"]);}...
回复

使用道具 举报

千问 | 2019-2-12 13:27:13 | 显示全部楼层
未初始化标量...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行