NetraSW
NetraSW

Reputation: 1395

NullReferenceException was unhandled by usercode in mvc application using linq to sql

I am calling below method in my controller.

 var comments = _blogService.GetBlogComments(bc.CommentParentID);//Controller code

class code:

public virtual IList<BlogComment> GetBlogComments(int CommentParentId)
        {
            if (CommentParentId != 0)
            {
                var query = from bc in _blogCommentRepository.Table
                            where bc.CommentParentID == CommentParentId
                            select bc;

                var comments = query.ToList();
                return comments;
            }
            else
                return null;
        }

I am getting error on

var query = from bc in _blogCommentRepository.Table
                            where bc.CommentParentID == CommentParentId
                            select bc;][1]

https://i.sstatic.net/uMWcV.jpg

Upvotes: 1

Views: 862

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

The first thing that comes to bind is that the _blogCommentRepository variable that you are using inside the GetBlogComments method is null. So make sure that this variable is initialized before using it:

_blogCommentRepository = new ...

Upvotes: 1

Related Questions