后台类:
public class PersonWork
{
private int workid;
private int empid;
private string worktime;
private string workcontent; public PersonWork()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public PersonWork(int empId,string workTime,string WorkContent)
{
this.empid=empId;
this.worktime=workTime;
this.workcontent=WorkContent;
}
public  SqlDataReader QueryByName(string pname)
{
    SqlDataReader dr=null;
SqlConnection con=null;
try
{
con= ConnectionSQLServer.GetConnection();
if(con.State != System.Data.ConnectionState.Open)
{
job_manager.WebMessageBox.ShowQuestion("数据库连接失败","!");
return null;
}

SqlCommand cmd=new SqlCommand("select e.userName,w.workTime,w.workcontent from personwork w,emp e where e.userName='"+pname+"' and e.empid=w.empid",con);
// job_manager.WebMessageBox.ShowInfo("select e.userName,w.workTime,w.workcontent from personwork w,emp e where e.userName='"+pname+"'and e.empid=w.empid","..");
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;

// if(dr.Read())
// {
// job_manager.WebMessageBox.ShowInfo(dr.GetString(0),"4545");
// job_manager.WebMessageBox.ShowInfo(dr.GetString(1),"4545");
// job_manager.WebMessageBox.ShowInfo(dr.GetString(2),"4545");
// }
}
catch(SqlException e)
{
job_manager.WebMessageBox.ShowQuestion(e.Message.ToString(),"!");
return null;
}
// finally
// {
// if (con.State == System.Data.ConnectionState.Open)
// {
// con.Close();
// con = null;
// }
// }

}
public bool QueryByTime(string time,int id)
{
SqlDataReader dr=null;
SqlConnection con=null;
try
{
con= ConnectionSQLServer.GetConnection();
if(con.State != System.Data.ConnectionState.Open)
{
job_manager.WebMessageBox.ShowQuestion("数据库连接失败","!");
return false;
}
SqlCommand cmd=new SqlCommand("select * from personwork where worktime='"+time+"'and empid='"+id+"'",con);
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.HasRows)
{
return true;
}
else
return false;
}
catch(Exception e)
{
job_manager.WebMessageBox.ShowQuestion(e.Message,"!");
}
finally
{
if(!dr.IsClosed)
{
dr.Close();
}
} return false;
}
 前台调用:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
btnSubmit.Attributes.Add("onclick","return confirm('是否确认添加')");
txtTime.Text=DateTime.Now.ToShortDateString().ToString();

}
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.btnSubmit.Click += new System.Web.UI.ImageClickEventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnSubmit_Click(object sender, System.Web.UI.ImageClickEventArgs e)


Emp emp=(Emp)Session["user"];
PersonWork pwork=new PersonWork(emp.EmpId,txtTime.Text,txtContent.Text);
// bool flag=false;
//
// flag=work.QueryByTime(txtTime.Text,emp.EmpId);
if(pwork.QueryByTime(txtTime.Text,emp.EmpId))
{
Response.Write("<script language='javascript'>alert('您已经写过总结,总结只允许写一次并且不能修改')");
return;
}
else
{

// bool flag1=false;
// flag1=pwork.InsertDialog(pwork);
if(pwork.InsertDialog(pwork))
{
Response.Write("<script language=javascript>alert('添加成功')</script>");
Response.Redirect("DialogQuery.aspx");
}
}
}
}
怎么编译的时候总说对象未实例化,就是在实例化PersonWork类出错,说“异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。”
我本来想调用方法判断数据库里是否有相同时间的纪录如果有就return
没有就添加,帮忙看看,我比较笨,没有查出来,谢谢各位

解决方案 »

  1.   

    flag=work.QueryByTime(txtTime.Text,emp.EmpId);
    这里应该是pwork.Query......吧
    但是编译时怎么会报对象未实例化啊
    是运行的时候吧?
      

  2.   

    恩,是运行的时候,说错话了
    报的是“异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例”
      

  3.   

    用Session前判断一下
    Session["user"] != null
    然后再转换
      

  4.   

    Click the link to solve your problem.Good luck!