c#调试时 出现错误 提示使用new关键字创建对象实例
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 System_user : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["username"]) == "")
        {
            Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
        }
        if (!IsPostBack)
        {
            this.username.Text = Session["username"].ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        stockClass sc = new stockClass();
        if (sc.validate(Request["userpwd"].ToString()))
        {
            Response.Write("<script>alert('密码中不能含有非法字符');history.back()</script>");
            Response.End();
        }
        SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
        strcon.Open();
        SqlCommand scd = new SqlCommand("update tb_user set userpwd='"+Request["userpwd"]+"',addtime='"+DateTime.Now+"' where username='" +Convert.ToString(Session["username"]) + "'", strcon);
        scd.ExecuteNonQuery();
        strcon.Close();
        Response.Write("<script>alert('密码修改成功');location='user_edit.aspx'</script>");
    }
}

解决方案 »

  1.   

    Session["username"] 可能为Null, 这导致ToString() 出错, 排查Session的赋值代码。
      

  2.   

    if ((Session["username"]==null || Convert.ToString(Session["username"]) == "")
            {
                Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
            }
      

  3.   

    上一个if 增加Session["username"]==null判断
      

  4.   

      if (Convert.ToString(Session["username"]) == "")
            {
                Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
            }
    改为
      if (Convert.ToString(Session["username"]) ==null)
            {
                Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
            }
      

  5.   

    改成下面形式if(Session["username"]!=null)
    {
        this.username.Text = Session["username"].ToString();
    }