为什么此Object Reference未设置为对象的实例?

时间:2017-02-09 21:44:24

标签: asp.net

在这个页面(NewStudent.aspx.cs)它工作正常,没有错误!

SDS.Student_SelectDataTable SDT = new SDS.Student_SelectDataTable();
SDSTableAdapters.Student_SelectTableAdapter Sadapter = new SDSTableAdapters.Student_SelectTableAdapter();

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnRegister1_Click(object sender, EventArgs e)
{
    lblDOB.Text = "";
    lblGender.Text = "";

    if (DrpDD.SelectedIndex == 0 && DrpMM.SelectedIndex == 0 && DrpYY.SelectedIndex == 0)
    {
        lblDOB.Text = "Select Your BirthDate";
    }
    else if (DrpGender.SelectedIndex == 0)
    {
        lblGender.Text = "Please select Gender";
    }
    else
    {
        int a = Sadapter.Insert(txtSErNo.Text, txtSfname.Text, txtSLname.Text, txtSadd.Text, txtScity.Text, txtSstate.Text, txtSpin.Text, Convert.ToDateTime(DrpDD.SelectedItem.Text + " " + DrpMM.SelectedItem.Text + " " + DrpYY.SelectedItem.Text), DrpGender.SelectedItem.ToString(), txtSMoNo.Text, txtSeid.Text, txtSUname.Text, txtSpass.Text);

        if (a == 1)
        {
            Session["SERNO"] = txtSErNo.Text;
            string test = Session["SERNO"].ToString();
            Response.Redirect("AddEducation.aspx");
            clean();

        }
    }
}

但是在这个页面上(AddEducation.aspx.cs)我不断得到“对象引用未设置为对象的实例”。这是此页面的代码

SDSTableAdapters.SELECT_ADDEDUTableAdapter EAdapter = new     SDSTableAdapters.SELECT_ADDEDUTableAdapter();


protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {          
         txtErlno.Text = Session["SERNO"].ToString();
    }
}


protected void btnsbmit_Click(object sender, EventArgs e)
{
    lblEdu.Text = "";
    lblBranch.Text = "";
    lblPassyr.Text = "";
    lblSkill.Text = "";
    lblmsg.Text = "";
    lblResume.Text = "";
    if(drpsem.SelectedIndex == 0)
    {
        lblsem.Text = "!!!";
    }
    else if (drpEdu.SelectedIndex == 0)
    {
        lblEdu.Text = "!!!";
     }
    else if (drpBranch.SelectedIndex == 0)
    {
        lblBranch.Text = "!!!";

    }
    else if (drpPassyear.SelectedIndex == 0)
    {
        lblPassyr.Text = "!!!";

    }
    else if (drpreqskill.SelectedIndex == 0)
    {
        lblSkill.Text = "!!!";
    }

    else
    {
        if (FUResume.HasFile)
        {
            FUResume.SaveAs(Server.MapPath("~/Resume/" + FUResume.FileName.ToString()));
            string resume = "~/Resume/" + FUResume.FileName.ToString();
            int ist = EAdapter.Insert(Convert.ToInt32(Session["SID"].ToString()), txtclgname.Text, txtErlno.Text, drpsem.SelectedItem.Text, drpEdu.SelectedItem.Text, drpBranch.SelectedItem.Text, drpPassyear.SelectedItem.Text, Convert.ToDouble(txtCPI.Text), Convert.ToDouble(txtCGPA.Text), drpreqskill.SelectedItem.Text, txtExSkill.Text, resume);

        if (ist == 1)
        {
                lblmsg.Text = "Add successfully..";
                clean();
                Response.Redirect("Home.aspx");
            }
        }
        else
        {
            lblResume.Text = "!!!";
            lblEdu.Text = "";
            lblBranch.Text = "";
            lblPassyr.Text = "";
        }
    }

请问有什么问题?请帮助。

1 个答案:

答案 0 :(得分:1)

一种可能是您没有检查SERNO Page_Load中的会话AddEducation.aspx.cs是否存在。如果不是,那么你会得到那个错误。

if (Session["SERNO"] != null)
{
     txtErlno.Text = Session["SERNO"].ToString();
}