用户代码NullReferenceException未将对象引用设置到对象的实例
using System;using System.Data;
using System.Data.SqlClient;
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;
namespace xueshengtonzhi
{
public partial class detail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack )
DataGridDataBind();
}
protected void DataGridDataBind()
{
string ID = "";
if (Request["ID"].ToString()!=null)
{
ID = Request["ID"].ToString();//取得上以页面传递过来的参数
}
SqlConnection conn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=1234;database=xiwangzhan");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from xstzb where id=" + ID, conn);
//打开数据连接
//创建数据读取器
try
{
SqlDataReader dr = cmd.ExecuteReader();
//如果取到数据
if (dr.Read())
{
head.Text = dr["Title"].ToString();
content.Text = dr["Content"].ToString();
man.Text = dr["Publicman"].ToString();
riqi.Text = Convert.ToDateTime(dr["Publictime"]).ToString();
}
}
catch (Exception error)
{
Response.Write(error.ToString());
}
finally{
//关闭数据连接
conn.Close();
}
运行时出现户代码NullReferenceException未将对象引用设置到对象的实例,各位高手帮忙看看哪有问题?谢谢啦!