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;
using System.IO;
public partial class UserWork_Bottom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string status="未读";
        string Name = SelectName();
        SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from Email,Employee where Email.Meetname='" + Name + "'  and status='" + status + "' ";
         SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            Response.Write("<bgsound   src='D:\\OA_Test\\你有新的消息,请注意查收!hun(NEW59).mp3' autostart='true'>"); 
        }
        con.Close();
        if (!IsPostBack)
        {
            Bind();
        }
    }
    protected void bnQuery_ServerClick(object sender, EventArgs e)
    {    }
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string Name=SelectName();
        string sql = "select * from Email,Employee where Email.Meetname='" + Name + "' and Email.sendname=Employee.username order by sid desc ";        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        //while (dr.Read())
        //{
        //    string Meetname = dr["Meetname"].ToString();
        //    string[] a;
        //    a = Meetname.Split(';');
        //    for (int i = 0; i < a.Length; i++)
        //    {
        //        string c = a[i].ToString();
        //        string b = ChaXun();
        //        if (c == b)
        //        {        //        }
        //    }        //}
        //con.Close();
    }
    public string SelectName()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select Name from Employee where username='"+Session["UserName"].ToString()+"'";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        string Name = dr.GetValue(0).ToString();
        return Name;
       
    }
    protected void bnQuery_ServerClick1(object sender, EventArgs e)
    {
        if (this.txFDate.Value == "" || this.txTDate.Value == "")
        {
            Response.Write("<script>alert('请选择时间!')</script>");
        }
        else
        {
            DateTime FD = Convert.ToDateTime(this.txFDate.Value);
            DateTime TD = Convert.ToDateTime(this.txTDate.Value);
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();            string sql = "select * from Email,Employee where Pubdate between '" + FD + "' and '" + TD + "' and sendname='" + Session["UserName"].ToString() + "' and Email.sendname=Employee.username";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "记录";
            con.Close();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        int sid = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        string status = "已查看";
        string sql = "update Email set Status='"+status+"' where sid="+sid+" ";
        SqlCommand com = new SqlCommand(sql,con);
        com.ExecuteNonQuery();
        con.Close();
        Response.Redirect("../MailRead1.aspx?sid="+sid+"");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        int sid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string sql = "select Appurtenance from Email where sid=" + sid + "";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        string ss=dr["Appurtenance"].ToString();
        string[] a = ss.Split(';');
        string url = "";
        for (int i = 0; i < a.Length; i++)
        {
            string b = a[i];
             url = Server.MapPath("~") + "\\upfiles\\" + b;
             break;
        }
      
        if(File.Exists(url))
        {
            File.Delete(url);
        }
        SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con1.Open();
        string sql1 = "delete from Email where sid=" + sid + "";
        SqlCommand com1 = new SqlCommand(sql1, con1);
        com1.ExecuteNonQuery();
        Response.Write("<script>alert('删除成功!')</script>");        con1.Close();
        Bind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }
    }
}
错误提示:string sql = "select Name from Employee where username='"+Session["UserName"].ToString()+"'";
——>用户代码为处理NullReferenceException    未将对象引用设置到对象的实例
麻烦各位高手帮忙。

解决方案 »

  1.   

    你用ToString()方法时,首先要检查页面中Session["UserName"]是否为空,如果为空就会报这个错的
      

  2.   

    说明你这个Session["UserName"]为空啊
      

  3.   

    Session["UserName"]==null?"":Session["UserName"].ToString()
      

  4.   

    怎样检查页面中Session["UserName"]是否为空?
      

  5.   

    Session["UserName"] == null这个就可以了
      

  6.   

    这种情况多是没有声明变量,突然出现了调用
    或者变量不是全局的
    很多ASP.net初学者都以为Page_Load方法是万能的
      

  7.   

    string sql = "select Name from Employee where username='"+Session["UserName"]==null?"":Session["UserName"].ToString()+"'";
      

  8.   

    string sql = "select Name from Employee where username='"+Session["UserName"]==null?"":Session["UserName"].ToString()+"'";
    我用这种方法,还是提示同样的错误,叫高手继续指点,谢谢!