Link to home
Start Free TrialLog in
Avatar of Jay
Jay

asked on

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

Why do I get this error at line24 ??

Line 23:             //fill pg w data
Line 24:             lblPrice.Text = "Price per unit: <br/>$ " + detail.eventPrice;
Line 25:             lblTitle.Text = detail.eventDetailName;
Line 26:             lblDescription.Text = detail.eventDesc;
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

because detail is null.  You are missing some line assigning a value for example detail = new Detail();
From message sent direct to me:

 I already assigned above!! But there is still an error :(

//get selected event's data
        if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);

            EventDetail Detail = new EventDetail();
            EventDetail detail = Detail.getEventDetail(id);
   
//fill pg w data
            lblPrice.Text = "Price per unit: <br/>$ " + detail.eventPrice;
            lblTitle.Text = detail.eventDetailName;
            lblDescription.Text = detail.eventDesc;
            lblItemNr.Text = id.ToString();
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);

            EventDetail Detail = new EventDetail();
            EventDetail detail = Detail.getEventDetail(id);
 if(detail == null)
{
  MessageBox.Show("No event found");
  return;
}

//fill pg w data
            lblPrice.Text = "Price per unit: <br/>$ " + detail.eventPrice;
            lblTitle.Text = detail.eventDetailName;
            lblDescription.Text = detail.eventDesc;
            lblItemNr.Text = id.ToString();
Avatar of Jay
Jay

ASKER

Here is the getEventDetail mthod in Class EventDetail if it helps

 public EventDetail getEventDetail(int eventdetailid)
    {
        EventDetail eveDetail = null;

        int eventtypeid, participants;
        string eventdetailname, eventdate, eventdesc, location, starttime, endtime, eventimage;
        decimal eventprice;

        string queryStr = "SELECT * FROM EventDetail WHERE EventDetail_ID = @EventDetail_ID";

        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        cmd.Parameters.AddWithValue("@EventDetail_ID", EventDetail_ID);

        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            eventtypeid = int.Parse(dr["EventTypeID"].ToString());
            eventdetailname = dr["eventDetailName"].ToString();
            eventdate = dr["eventDate"].ToString();
            eventprice = decimal.Parse(dr["eventPrice"].ToString());
            eventdesc = dr["eventDesc"].ToString();
            location = dr["eventLocation"].ToString();
            starttime = dr["startTime"].ToString();
            endtime = dr["endTime"].ToString();
            eventimage = dr["eventImage"].ToString();
            participants = int.Parse(dr["eventParticipants"].ToString());

            eveDetail = new EventDetail(eventdetailid, eventtypeid, eventdetailname, eventdate, eventprice, eventdesc, location, starttime, endtime, eventimage, participants);
        }
        else { eveDetail = null; }

        conn.Close();
        dr.Close();
        dr.Dispose();

        return eveDetail;
    }
public EventDetail getEventDetail(int eventdetailid)
....
cmd.Parameters.AddWithValue("@EventDetail_ID", EventDetail_ID);

You pass in an int value (eventdetailid) into the function then use a totally different value (EventDetail_ID) as a parameter.  I expect there is no event with the ID you actually use to filter the data source, so your function returns null and then you get the error you posted.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay

ASKER

It works now. Thankyou !!!x