Click here to Skip to main content
15,909,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
1.  public BoardPost GetPostByID(Int64 PostID)
    {
2.           BoardPost result = null;
3.           using(FisharooDataContext dc = _conn.GetContext())
            {
4.               BoardPost post = dc.BoardPosts.Include("Account").Where(p =>  
                     p.PostID == PostID).FirstOrDefault();
5.               result = post;
6.               result.Account = post.Account;
            }
7.           return result;
    }


I get error at line no 6 probably due to wrong coding at line no 4 or due to some missing references. Please help me out. Thank you dear friends.

[edit]Added pre tag and unchecked Treat my content as plain text, not as HTML[/edit]
Posted
Updated 22-Feb-12 5:33am
v4

1 solution

It is clear that your BoardPost post = dc.BoardPosts... line is returning null so your result.Account = post.Account is failing with post being null.

Whenever you do something to get an object back, always ensure that the object exists before trying to use it.
C#
if( post != null )
   result.Account = post.Account;


As to the syntax of the linq, I don't see anything wrong with it off the top of my head.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900