异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 25: SqlDataReader sdr = data.ExcuteReader("select id,keywordcategoryname from zhaobiaokeywordcategory where prarentid=1");
行 26:
行 27: while (sdr.Read())
行 28: {
行 29: keylist += sdr["keywordcategoryname"].ToString() + " ";
源文件
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 zhaobiao_control_searchoption : System.Web.UI.UserControl
{
public string knowkeylist = "";
public string hotkeylist = "";
public string keylist = ""; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataReader sdr1;
SqlDataReader sdr = data.ExcuteReader("select id,keywordcategoryname from zhaobiaokeywordcategory where prarentid=1"); while (sdr.Read())
{
keylist += sdr["keywordcategoryname"].ToString() + " ";
sdr1 = data.ExcuteReader("select keyword from zhaobiaokeyword where categoryid=" + sdr["id"].ToString() + " order by orderid");
while (sdr1.Read())
{
keylist += "<a href=\"javascript:searchZkey('" + sdr1[0].ToString() + "')\">" + sdr1[0].ToString() + "</a> ";
}
keylist += "<br />";
}
sdr.Close();
sdr = data.ExcuteReader("select keyword from zhaobiaokeyword where categoryid=2");
while (sdr.Read())
{
knowkeylist += "<a href=\"javascript:searchKnowkey('" + sdr[0].ToString() + "') \">" + sdr[0].ToString() + "</a> ";
}
sdr.Close();
sdr = data.ExcuteReader("select keyword from zhaobiaokeyword where categoryid=3");
while (sdr.Read())
{
hotkeylist += "<a href=\"javascript:searchZkey('" + sdr[0].ToString() + "')\">" + sdr[0].ToString() + "</a> ";
}
} }
}
data.cs 源码为
public static SqlDataReader ExcuteReader(string sql)
{ SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand(sql,conn); try
{
conn.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection); }
catch (Exception ex)
{
return null;
}
}
public static SqlDataReader ExcuteReader(string sql, SqlParameter para)
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.Add(para);
try
{
conn.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection); }
catch (Exception ex)
{
return null;
}
}
public static SqlDataReader ExcuteReader(string sql, SqlParameter[] para)
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand(sql, conn);
foreach (SqlParameter para1 in para)
{
comm.Parameters.Add(para1);
}
try
{
conn.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection); }
catch (Exception ex)
{
return null;
}
}
之前用的是sql2000数据库 没有问题 现在改成sql2005数据库就报了这种错误???急是什么问题呢??
行 25: SqlDataReader sdr = data.ExcuteReader("select id,keywordcategoryname from zhaobiaokeywordcategory where prarentid=1");
行 26:
行 27: while (sdr.Read())
行 28: {
行 29: keylist += sdr["keywordcategoryname"].ToString() + " ";
源文件
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 zhaobiao_control_searchoption : System.Web.UI.UserControl
{
public string knowkeylist = "";
public string hotkeylist = "";
public string keylist = ""; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataReader sdr1;
SqlDataReader sdr = data.ExcuteReader("select id,keywordcategoryname from zhaobiaokeywordcategory where prarentid=1"); while (sdr.Read())
{
keylist += sdr["keywordcategoryname"].ToString() + " ";
sdr1 = data.ExcuteReader("select keyword from zhaobiaokeyword where categoryid=" + sdr["id"].ToString() + " order by orderid");
while (sdr1.Read())
{
keylist += "<a href=\"javascript:searchZkey('" + sdr1[0].ToString() + "')\">" + sdr1[0].ToString() + "</a> ";
}
keylist += "<br />";
}
sdr.Close();
sdr = data.ExcuteReader("select keyword from zhaobiaokeyword where categoryid=2");
while (sdr.Read())
{
knowkeylist += "<a href=\"javascript:searchKnowkey('" + sdr[0].ToString() + "') \">" + sdr[0].ToString() + "</a> ";
}
sdr.Close();
sdr = data.ExcuteReader("select keyword from zhaobiaokeyword where categoryid=3");
while (sdr.Read())
{
hotkeylist += "<a href=\"javascript:searchZkey('" + sdr[0].ToString() + "')\">" + sdr[0].ToString() + "</a> ";
}
} }
}
data.cs 源码为
public static SqlDataReader ExcuteReader(string sql)
{ SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand(sql,conn); try
{
conn.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection); }
catch (Exception ex)
{
return null;
}
}
public static SqlDataReader ExcuteReader(string sql, SqlParameter para)
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.Add(para);
try
{
conn.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection); }
catch (Exception ex)
{
return null;
}
}
public static SqlDataReader ExcuteReader(string sql, SqlParameter[] para)
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand(sql, conn);
foreach (SqlParameter para1 in para)
{
comm.Parameters.Add(para1);
}
try
{
conn.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection); }
catch (Exception ex)
{
return null;
}
}
之前用的是sql2000数据库 没有问题 现在改成sql2005数据库就报了这种错误???急是什么问题呢??
解决方案 »
- 我为什么会得不到界面上的textbox呢?【F5在线中】
- treeview显示树形表时,节点重复出现两次(在线等)
- datalist的动态绑定数据
- [求助]TreeView中点击根节点,无刷新展开子节点是如何实现的。
- 调试的简单问题
- 请问我用Microsoft Visual Studio 2005编完程序运行以后为什么没有生成dll文件也没有bin目录
- 在线的大侠帮忙啊,实在不知道怎么改!
- 網站資料庫設計問題...歡迎討論....
- 小妹有问题请教各位高手,谢谢!
- 高手帮忙,高分相赠送
- 如何实现GridView的行背景色渐变效果。就像CSDN中“排行榜”里面的数据列一样。鼠标移动背景色渐变。
- 为什么分页可以,但是点下一页就不显示数据,就报错?
也许sdr["keywordcategoryname"]=null 这个就检查一下 keywordcategoryname 和数据库是不是一致另外 要学会调试
SqlDataReader sdr = data.ExcuteReader("select id,keywordcategoryname from zhaobiaokeywordcategory where prarentid=1");sdr 为空了,,你断点调试一步一步进去看
if(sdr.HasRows)
{
while (sdr.Read())
{}
}
检查data.ExcuteReader
这句有点问题,当sdr["keywordcategoryname"]为空时tostring()就会出现这个错误,为空判断一下
2 javascript:searchZkey('" + sdr1[0].ToString() + "')-->javascript:searchZkey(" + sdr1[0].ToString() + ")
3 keywordcategoryname这列 是不是没you
4 keylist-->stringbuilder