Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am always getting error

object reference not set to the instance of object


when checking if (Session["DETAILS"].ToString() == "NEW")



My source code is :
C#
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;

public partial class SCDetails : System.Web.UI.Page
{

    Properties p = new Properties();
    Functions Fun = new Functions();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack == false)
            {
                FillDD();
                if (Session["DETAILS"].ToString() == "NEW")
                {
                    Label1.Text = "NEW SUB CATEGORY DETAILS [ADD]";
                    DropDownList1.Focus();
                }
                else if (Session["DETAILS"].ToString() == "EDIT")
                {
                    if (Session["SCID"].ToString() != "")
                    {
                        Label1.Text = "EDIT SUB CATEGORY DETAILS [EDIT]";
                        Label12.Text = Session["SCID"].ToString();
                        Session["SCID"] = "";
                        GetDetails();
                        DropDownList1.Focus();
                    }
                }
            }
        }
        catch
        {

        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Label12.Text == "")
        {
            Boolean cn;
            cn = Fun.ChkvalueExist("SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE CNAME='" + DropDownList1.SelectedItem.Text + "' AND SCNAME='" + TextBox1.Text + "'");
            if (cn == false)
            {
                p.CNAME = DropDownList1.SelectedItem.Text;
                p.SGNAME = TextBox1.Text;
                p.DESC = TextBox2.Text;
                string sql = "INSERT INTO CMS_TB_MASTER_SUBCATEGORY VALUES('" + p.CNAME + "','" + p.SGNAME + "','" + p.DESC + "')";
                Fun.Insert(sql);
                //fun.INFYDetails(p);
                Clear();
                Response.Write("<script language='javascript'>window.opener.document.getElementById('Button4').click();window.close();window.location='SubCategory.aspx';</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>window.alert('This sub category name already exists !');</script>");
            }
        }
        else
        {
            Boolean cn, cn1;
            cn = Fun.ChkvalueExist("SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE SCID='" + Label12.Text + "' AND CNAME='" + DropDownList1.SelectedItem.Text + "' AND SCNAME='" + TextBox1.Text + "'");
            cn1 = Fun.ChkvalueExist("SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE SCID!='" + Label12.Text + "' AND CNAME='" + DropDownList1.SelectedItem.Text + "' AND SCNAME='" + TextBox1.Text + "'");
            if ((cn == true && cn1 == false) || (cn == false && cn1 == false))
            {
                p.CNAME = DropDownList1.SelectedItem.Text;
                p.SGNAME = TextBox1.Text;
                p.DESC = TextBox2.Text;
                string sql = "UPDATE CMS_TB_MASTER_SUBCATEGORY SET CNAME='" + p.CNAME + "',SCNAME='" + p.SGNAME + "', DESCP='" + p.DESC + "' WHERE SCID='" + Label12.Text + "'";
                Fun.Insert(sql);
                //fun.UDFYDetails(p);
                Clear();
                //Response.Write("<script language='javascript'>window.opener.document.getElementById('Button4').click();window.close();window.location='SubCategory.aspx';</script>");
                Response.Redirect("SubCategory.aspx");
                GetDetails();
            }
            else
            {
                Response.Write("<script language='javascript'>window.alert('This sub category name already exists !');</script>");
            }
        }
    }

    #region Functions

    public void FillDD()
    {
        string sql = "SELECT CNAME,CID FROM CMS_TB_MASTER_CATEGORY ORDER BY CNAME";
        Fun.BDD_WITH_ID(DropDownList1, sql, "--SELECT--", "CID", "CNAME");
    }

    public void GetDetails()
    {
        DataSet DS = new DataSet();
        string sql = "SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE SCID='" + Label12.Text + "'";
        DS = Fun.DBSelect(sql);
        if (DS.Tables[0].Rows.Count > 0)
        {
            FillDD();
            DropDownList1.SelectedValue = DS.Tables[0].Rows[0]["CNAME"].ToString();
            TextBox1.Text = DS.Tables[0].Rows[0]["SCNAME"].ToString();
            TextBox2.Text = DS.Tables[0].Rows[0]["DESCP"].ToString();
        }
        else
        {
            Clear();
            Button2.Enabled = false;
        }
    }

    public void Clear()
    {
        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        TextBox2.Text = "";
    }

    #endregion
     
    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (Label12.Text == "")
        {
            Clear();
        }
        else
        {
            Response.Redirect("SubCategory.aspx");
        }
    }
}
Posted
Updated 18-Sep-12 2:37am
v2
Comments
Timberbird 18-Sep-12 8:38am    
Because you never set Session["DETAILS"] and it's always null, maybe?

Before using any session information, The null check should be there.
C#
if(Session["DETAILS"]!=null)
{
 //Code goes here.
}
 
Share this answer
 
v3
You should always check your sessions before using it, otherwise it'll give NullReferanceException.
Try this:
C#
if (IsPostBack == false)
{
    FillDD();
    if(Session["DETAILS"] != null){
        if (Session["DETAILS"].ToString() == "NEW")
        {
            Label1.Text = "NEW SUB CATEGORY DETAILS [ADD]";
            DropDownList1.Focus();
        }
        else if (Session["DETAILS"].ToString() == "EDIT")
        {
            if (Session["SCID"].ToString() != "")
            {
                Label1.Text = "EDIT SUB CATEGORY DETAILS [EDIT]";
                Label12.Text = Session["SCID"].ToString();
                Session["SCID"] = "";
                GetDetails();
                DropDownList1.Focus();
            }
        }
    }
}



--Amit
 
Share this answer
 
set session variable in ur global aspx page before using it in page load event. u haven't initialized it ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900