Link to home
Start Free TrialLog in
Avatar of Amjad Fiaz
Amjad Fiaz

asked on

System.NullReferenceException: Object reference not set to an instance of an object.

   protected void Page_Load(object sender, EventArgs e)
        {

            CDT = Cadapter.SELECT_SEARCH_COMPANY(Session["JOBCATE"].ToString(), Session["QUALI"].ToString(), Session["skill"].ToString());
            if (CDT != null)
            {
                if (CDT.Rows.Count > 0)
                {
                    Session["CID"] = CDT.Rows[0]["CID"].ToString();
                }
                else
                {
                    DataList3.DataSource = CDT;
                    DataList3.DataBind();
                }
            }

        }

Open in new window

Avatar of HainKurt
HainKurt
Flag of Canada image

Avatar of Amjad Fiaz
Amjad Fiaz

ASKER

but it is a searching from database how i do it?
which line is giving that error?
put a breakpoint and see all the variables and expressions on that line to see what is wrong
Avatar of Ryan Chong
from your codes posted, without the actual logic, few variables to check and make sure they are not null.

  • Session["JOBCATE"]
  • Session["QUALI"]
  • Session["skill"]
  • CDT.Rows[0]["CID"]

As mentioned by HainKurt, place the break points so that you know which line hits the error
Your first line makes a lot of assumptions without checking the nullability of many object/property references:
CDT = Cadapter.SELECT_SEARCH_COMPANY(Session["JOBCATE"].ToString(), Session["QUALI"].ToString(), Session["skill"].ToString());

Open in new window


As others have stated, you need to check your many references, namely:
  • Cadapter
  • Session["JOBCATE"]
  • Session["QUALI"]
  • Session["skill"]

This would essentially look like:
protected void Page_Load(object sender, EventArgs e)
{
    var jobcate = Session["JOBCATE"];
    var quali = Session["QUALI"];
    var skill = Session["skill"];

    if (Cadapter != null && jobcate != null && quali != null && skill != null)
    {
        CDT = Cadapter.SELECT_SEARCH_COMPANY(jobcate.ToString(), quali.ToString(), skill.ToString());
        if (CDT != null)
        {
            if (CDT.Rows.Count > 0)
            {
                Session["CID"] = CDT.Rows[0]["CID"].ToString();
            }
            else
            {
                DataList3.DataSource = CDT;
                DataList3.DataBind();
            }
        }
    }
}

Open in new window


-saige-
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.