Property accessor 'Title' on object x threw the following exception:'Object reference not set to an instance of an object

StackOverflow https://stackoverflow.com/questions/18217359

Question

I am getting this error on my DataBind Command I am trying to populate a gridview from a List using asp and c#. The Scenario is repeater populates category links on sidebar, based on selection will populate a gridview in the main div. It worked fine when I used a dataset as my datasource but this scenario I am using generics and I don't know where the error is coming from. The List gets populated just cant databind it

public partial class Products : System.Web.UI.Page
{
    string ItemNumber;
    string ItemDescription;
    string  PrePrice;
    string Size;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopCategories();

        }
    }  
private List<Products> GetCategoryDetails(string HiddenField1)
    {
        DataTable table = new DataTable();
        table = new BizLayer().grabdata(HiddenField1);

        List<Products> list = new List<Products>();
        foreach (DataRow dr in table.Rows)
        {
            Products products = new Products();
            products.ItemNumber = dr["Item #"].ToString();
            products.ItemDescription = dr["Item Description"].ToString();
            products.PrePrice = dr["Pre Price"].ToString();
            products.Size = dr["Size"].ToString();
            list.Add(products);
        }
        return list;
    }
 private void PopDetails()
    {
        DetailsGridView.DataSource = GetCategoryDetails(HiddenField1.Value);

Error Here DetailsGridView.DataBind();

    }  

  protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {       
        //Get Command Arguement from repeater OnItemCommand
        if (e.CommandName == "cmd")
        {
            HiddenField1.Value = (e.CommandArgument).ToString();             

             GetCategoryDetails(HiddenField1.Value);
             PopDetails();
        }            
    }

    protected void DetailsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

            DetailsGridView.PageIndex = e.NewPageIndex;
            //  DataSource  
            DetailsGridView.DataSource = GetCategoryDetails(HiddenField1.Value);

            DetailsGridView.DataBind();            
    }

 <asp:GridView ID="DetailsGridView" AllowPaging="true" PageSize="3" CssClass="dataGrid" 
  RowStyle-  CssClass="chartItemStyle" AlternatingRowStyle-CssClass="chartAlternatingItemStyle" 
  HeaderStyle-  CssClass="chartHeaderStyle" runat="server"   OnPageIndexChanging="DetailsGridView_PageIndexChanging">   </asp:GridView>
Was it helpful?

Solution

public partial class Products : System.Web.UI.Page
{
   public class Products2
   {
       public string ItemNumber { get; set; }
       public string ItemDescription { get; set; }
       public string PrePrice { get; set; }
       public string Size { get; set; }
   }
//....

Then:

List<Products2> list = new List<Products2>();
foreach (DataRow dr in table.Rows)
{
     Products2 products = new Products2();
     products.ItemNumber = dr["Item #"].ToString();
     products.ItemDescription = dr["Item Description"].ToString();
     products.PrePrice = dr["Pre Price"].ToString();
     products.Size = dr["Size"].ToString();
     list.Add(products);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top