locked
NullreferenceException was unhandled by user code :Object reference not set to an instance of an object RRS feed

  • Question

  • User-1393440807 posted

    Hi There,
    I am working a small project and using devexpress tool.I am not getting correct code to get the value of a column in my code.Please,help me

    Error here: string cid = row.EvalDataItem("HouseImageID").ToString();

    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BuckshawObjects.Global;
    using BuckshawObjects.Objects;
    using DevExpress.Web.ASPxEditors;
    using System.IO;
    using DevExpress.Web.ASPxDataView;
    using System.Collections;
    using DevExpress.Web.ASPxGridView;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using DevExpress.XtraRichEdit.API.Native;
    using System.Drawing;


    namespace Buckshaw.Backend
    {
    public partial class WebForm5 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
    {
    string constr = ConfigurationManager.ConnectionStrings["BuckshawHomesConnectionString"].ConnectionString;

    ASPxCheckBox chkStatus = (ASPxCheckBox)sender;
    //ASPxDataView row = (ASPxDataView)chkStatus.NamingContainer;
    DataViewItemTemplateContainer row=(DataViewItemTemplateContainer)chkStatus.NamingContainer;
    //string cid = row.Controls[0].ToString();
    //string cod =(row.ItemIndex+1).ToString();
    //string cod = row.ItemIndex.ToString();
    //string cod = dataView.FindControl("HouseImageID").ToString();

    //string cid = dataView.Controls[0].ToString();
    //string cod = dataView.Items[0].DataItem.ToString();
    string cid = row.EvalDataItem("HouseImageID").ToString();
    //string cid = "True";
    bool status = chkStatus.Checked;


    string query = "UPDATE [tblHouseImage] SET Active = @Approved WHERE HouseImageID = @HouseImageID";
    //WHERE HouseImageID = @HouseImageID"
    SqlConnection con = new SqlConnection(constr);
    SqlCommand com = new SqlCommand(query, con);
    com.Parameters.AddWithValue("@Approved", status);
    com.Parameters.AddWithValue("@HouseImageID", cid);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    dataView.DataBind();



    }
    }
    }

    Saturday, March 15, 2014 3:11 AM

Answers

  • User281315223 posted

    It's important to always check within code to see if a possibly null value exists to avoid exceptions like these as this occured because you attempted to use the ToString() method on a null object. 

    // Grab your House Image (if possible)
    var houseImage = row.EvalDataItem("HouseImageID");
    
    // Check if it exists
    if(houseImage != null)
    {
        // If so, then store the appropriate value you needed
        string cid = houseImage.ToString();
    }
    else
    {
        // It didn't exist and would have thrown an error otherwise
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 15, 2014 9:49 AM

All replies

  • User1208776063 posted

    I'm not an expert in devExpress, but here is what I suggest. 

    dps2305

    string cid = row.EvalDataItem("HouseImageID").ToString();

    Delete above line and set Checkbox value to HouseImageID in markup as value='<%# Eval("HouseImageID") %>'. So, that will bind the value and you retrieve the value as

    dps2305

    com.Parameters.AddWithValue("@HouseImageID", cid);

    com.Parameters.AddWithValue("@HouseImageID", chkStatus.Value);

    Saturday, March 15, 2014 9:19 AM
  • User281315223 posted

    It's important to always check within code to see if a possibly null value exists to avoid exceptions like these as this occured because you attempted to use the ToString() method on a null object. 

    // Grab your House Image (if possible)
    var houseImage = row.EvalDataItem("HouseImageID");
    
    // Check if it exists
    if(houseImage != null)
    {
        // If so, then store the appropriate value you needed
        string cid = houseImage.ToString();
    }
    else
    {
        // It didn't exist and would have thrown an error otherwise
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 15, 2014 9:49 AM