-1

i want to bind gridview with my custom entity whitch i populate from data base but i get error Object reference not set to an instance of an object i know the error is from my gridlink class when select new and want to set to link property in this line : link = { linkName = "tyr", linkSrc = "ytr" }, because when i earase it the error stop and gridview bind thanks

  public class gridcolumns
{

    public decimal cost { get; set; }
    public Int32 count { get; set; }
    public gridlink link { get; set; }

    public gridcolumns()
    {

        // TODO: Complete member initialization
    }
}

public class gridlink
{
    public string linkName { get; set; }
    public string linkSrc { get; set; }

    public gridlink()
    {

    }
}
 protected void Page_Load(object sender, EventArgs e)
{
    Data281DataContextDataContext conx = new Data281DataContextDataContext();
    List<tbl_2_CheckReqNo_NotValid> allresult = conx.tbl_2_CheckReqNo_NotValids.ToList();
    gridcolumns lastMantWithDate = new gridcolumns();
    if (Request.QueryString.Count == 0)
    {
        var lastMantWithDaste = from pe in allresult //where allresult != null
                                orderby Convert.ToDecimal(pe.mandeh) descending
                                group pe by pe.mant into grouped
                                where grouped != null
                                select new gridcolumns
                                {
                                    link = { linkName = "tyr", linkSrc = "ytr" },
                                    cost = grouped.Sum(g => Convert.ToDecimal(g.mandeh)),
                                    count = grouped.Count(),

                                };

        GrdOstan.DataSource = lastMantWithDaste;
        GrdOstan.DataBind();
    }
Mojtaba Pourmirzaei
  • 306
  • 1
  • 6
  • 17
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –  Aug 29 '12 at 07:34
  • What are the types of `pe.mandeh` and `pe.mant`? – Daniel Hilgarth Aug 29 '12 at 07:35
  • are you shure, that this even compiles? i believe that `link = { linkName = "tyr", linkSrc = "ytr" },` should throw an error ... –  Aug 29 '12 at 07:37

4 Answers4

4

You should be instantiating a gridlink like this:

link = new gridlink { linkName = "tyr", linkSrc = "ytr" },

Consider that for each item your query produces you create a gridcolumns object. The link property of that object is initially null; you should set it to new gridlink before trying to use it at all.

Jon
  • 428,835
  • 81
  • 738
  • 806
3

You probably need

link = new gridLink { linkName = "tyr", linkSrc = "ytr" }

instead of

link = { linkName = "tyr", linkSrc = "ytr" }
Graham Clark
  • 12,886
  • 8
  • 50
  • 82
1

Replace this:

select new gridcolumns
           {
           link = { linkName = "tyr", linkSrc = "ytr" },

With:

select new gridcolumns
           {
           link = new gridlink  { linkName = "tyr", linkSrc = "ytr" },

You need to instantiate a new object of type gridLink using the new keyword

Habib
  • 219,104
  • 29
  • 407
  • 436
0

In your line:

link = { linkName = "tyr", linkSrc = "ytr" }

You need "new gridlink", like this:

link = new gridlink { linkName = "tyr", linkSrc = "ytr" }

Personally, I think it's unfortunate that your code compiled. If I tried:

List<gridlink> links = new List<gridlink>();
links.Add({ linkName = "tyr", linkSrc = "ytr" });

I would correctly get a bevy of syntax errors.

Adding new gridlink should resolve this problem.

Jonatan
  • 2,734
  • 2
  • 22
  • 33