未找到引用对象的实例,在线等~~~

frank2828 2012-06-01 08:14:57
网页传参,有两个参数,代码如下,两个参数 不是同时传过来的
代码如下:

这两个参数的意思是在其他页面点击链接,有的链接传的是categoryId,有的是Id,所有一次只获取其中一个,但是不确定是哪个,所有总有一个找不到对象,我判断了,断点总是没到下面判断就报异常,请高手指点!

private void DataRead()
{
//categoryId为 baike_category表主键
string categoryId = Request.QueryString["categoryId"].ToString();
//newsId为baike_details表主键
string newsId = Request.QueryString["Id"].ToString();





BaikeDetailBusiness business = new BaikeDetailBusiness();



if (!string.IsNullOrEmpty(newsId))
{

BaikeDetail content = business.GetEntity(int.Parse(newsId));
ltitle.Text = content.Category_name;
if (content.CreateDate.HasValue)
{


lCreateDate.Text = "时间:" + content.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
lCreateDate.Text = "";
}
ltitle.Text = content.BaikeName;
lContent.Text = content.Description;

lCategoryName.Text = new BaikeCategoryBusiness().GetEntity(content.CategoryId).CategoryName;
business.UpdateClickNumber(int.Parse(newsId));
}
else if (!string.IsNullOrEmpty(categoryId))
{
BaikeDetail content2 = business.GetEntity2(int.Parse(categoryId));
ltitle.Text = content2.Category_name;
if (content2.CreateDate.HasValue)
{


lCreateDate.Text = "时间:" + content2.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
lCreateDate.Text = "";
}
ltitle.Text = content2.BaikeName;
lContent.Text = content2.Description;

lCategoryName.Text = new BaikeCategoryBusiness().GetEntity(int.Parse(categoryId)).CategoryName;
business.UpdateClickNumber2(int.Parse(categoryId));
}
}
...全文
188 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
licai1210 2012-06-01
  • 打赏
  • 举报
回复
你那F11调试到这个方法试试
frank2828 2012-06-01
  • 打赏
  • 举报
回复
这是DAL层 代码:

public BaikeDetail GetEntity(int Id)
{
BaikeDetail baikeDetail = new BaikeDetail();
Database database = DatabaseFactory.CreateDatabase();
string sql = " select baike_name,summary,description,keywords,category_id,is_show,is_recommend,is_delete,click_number,create_date,img from baike_detail where Id=@Id";
DbCommand cmd = database.GetSqlStringCommand(sql);
database.AddInParameter(cmd, "@Id", DbType.Int32, Id);
using (IDataReader reader = database.ExecuteReader(cmd))
{
if (reader.Read())
{
baikeDetail.Id = Id;
baikeDetail.CategoryId = int.Parse(reader["category_id"].ToString());
baikeDetail.ClickNumber = int.Parse(reader["click_number"].ToString());
baikeDetail.CreateDate = DateTime.Parse(reader["create_date"].ToString());
baikeDetail.Description = reader["description"].ToString();
baikeDetail.IsDelete= bool.Parse(reader["is_delete"].ToString());
baikeDetail.IsRecommend = bool.Parse(reader["is_recommend"].ToString());
baikeDetail.IsShow = bool.Parse(reader["is_show"].ToString());
baikeDetail.Keywords = reader["keywords"].ToString();
baikeDetail.BaikeName = reader["baike_name"].ToString();
baikeDetail.Summary = reader["summary"].ToString();
baikeDetail.Img = reader["img"].ToString();
//baikeDetail.Category_name = reader["category_name"].ToString();
}
}
return baikeDetail;
}
public BaikeDetail GetEntity2(int Id)
{
BaikeDetail baikeDetail = new BaikeDetail();
Database database = DatabaseFactory.CreateDatabase();
string sql = " select baike_name,summary,description,keywords,Id,is_show,is_recommend,is_delete,click_number,create_date,img from baike_detail where category_id=@category_id";
DbCommand cmd = database.GetSqlStringCommand(sql);
database.AddInParameter(cmd, "@category_id", DbType.Int32, Id);
using (IDataReader reader = database.ExecuteReader(cmd))
{
if (reader.Read())
{
baikeDetail.Id = int.Parse(reader["Id"].ToString());
baikeDetail.CategoryId = Id;
baikeDetail.ClickNumber = int.Parse(reader["click_number"].ToString());
baikeDetail.CreateDate = DateTime.Parse(reader["create_date"].ToString());
baikeDetail.Description = reader["description"].ToString();
baikeDetail.IsDelete = bool.Parse(reader["is_delete"].ToString());
baikeDetail.IsRecommend = bool.Parse(reader["is_recommend"].ToString());
baikeDetail.IsShow = bool.Parse(reader["is_show"].ToString());
baikeDetail.Keywords = reader["keywords"].ToString();
baikeDetail.BaikeName = reader["baike_name"].ToString();
baikeDetail.Summary = reader["summary"].ToString();
baikeDetail.Img = reader["img"].ToString();
baikeDetail.Category_name = reader["category_name"].ToString();
}
}
return baikeDetail;
}
frank2828 2012-06-01
  • 打赏
  • 举报
回复
content2.CreateDate.HasValue这个就是Bool值~~~
frank2828 2012-06-01
  • 打赏
  • 举报
回复
还没到那一步啊 在BaikeDetail content2 = business.GetEntity2(int.Parse(categoryId));
在这个位置报的错啊 我断点了就这地方,还没到下面呢
licai1210 2012-06-01
  • 打赏
  • 举报
回复
你应该理清楚逻辑在写,学会自己断点调试
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

这是个啥意思 这里哪有Bool值
[/Quote]
if里面的返回值必须是bool值,但是content2.CreateDate.HasValue的返回值不是bool,当然报错了。
frank2828 2012-06-01
  • 打赏
  • 举报
回复
这是个啥意思 这里哪有Bool值
frank2828 2012-06-01
  • 打赏
  • 举报
回复
改成这样了//categoryId为 baike_category表主键
string categoryId = Request.QueryString["categoryId"]as string;
//newsId为baike_details表主键
string newsId = Request.QueryString["Id"]as string;
BaikeDetailBusiness business = new BaikeDetailBusiness();

但是报了个新异常异常详细信息: System.FormatException: 该字符串未被识别为有效的布尔值。

源错误:


行 50: else if (!string.IsNullOrEmpty(categoryId))
行 51: {
行 52: BaikeDetail content2 = business.GetEntity2(int.Parse(categoryId));
行 53: ltitle.Text = content2.Category_name;
行 54: if (content2.CreateDate.HasValue)在52行

  • 打赏
  • 举报
回复
if (!string.IsNullOrEmpty(Request.QueryString["Id"].ToString()))
else if (!string.IsNullOrEmpty(Request.QueryString["categoryId"].ToString()))

将这两行的.ToString()去掉
frank2828 2012-06-01
  • 打赏
  • 举报
回复
////categoryId为 baike_category表主键
//string categoryId = Request.QueryString["categoryId"].ToString();
////newsId为baike_details表主键
//string newsId = Request.QueryString["Id"].ToString();
BaikeDetailBusiness business = new BaikeDetailBusiness();
if (!string.IsNullOrEmpty(Request.QueryString["Id"].ToString()))
{
BaikeDetail content = business.GetEntity(int.Parse(Request.QueryString["Id"].ToString()));
ltitle.Text = content.Category_name;
if (content.CreateDate.HasValue)
{
lCreateDate.Text = "时间:" + content.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
lCreateDate.Text = "";
}
ltitle.Text = content.BaikeName;
lContent.Text = content.Description;

lCategoryName.Text = new BaikeCategoryBusiness().GetEntity(content.CategoryId).CategoryName;
business.UpdateClickNumber(int.Parse(Request.QueryString["Id"].ToString()));
}
else if (!string.IsNullOrEmpty(Request.QueryString["categoryId"].ToString()))
{
BaikeDetail content2 = business.GetEntity2(int.Parse(Request.QueryString["categoryId"].ToString()));
ltitle.Text = content2.Category_name;
if (content2.CreateDate.HasValue)
{
lCreateDate.Text = "时间:" + content2.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
lCreateDate.Text = "";
}
ltitle.Text = content2.BaikeName;
lContent.Text = content2.Description;

lCategoryName.Text = new BaikeCategoryBusiness().GetEntity(int.Parse(Request.QueryString["categoryId"].ToString())).CategoryName;
business.UpdateClickNumber2(int.Parse(Request.QueryString["categoryId"].ToString()));
}
}

依然报这个错误异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 26: //string newsId = Request.QueryString["Id"].ToString();
行 27: BaikeDetailBusiness business = new BaikeDetailBusiness();
行 28: if (!string.IsNullOrEmpty(Request.QueryString["Id"].ToString()))
行 29: {
行 30: BaikeDetail content = business.GetEntity(int.Parse(Request.QueryString["Id"].ToString()));

  • 打赏
  • 举报
回复
if (!string.IsNullOrEmpty(Request.QueryString["categoryId"]) && !string.IsNullOrEmpty(Request.QueryString["ID"]))
{ }
else 跳转到指定页面
licai1210 2012-06-01
  • 打赏
  • 举报
回复
string categoryId = Request.QueryString["categoryId"] as string;
string newsId = Request.QueryString["Id"] as string;
换成这样就可以了
  • 打赏
  • 举报
回复
加个判断
if(Request.QueryString["categoryId"]!=null)

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧