none
Object reference not set to an instance of an object. Datacontext RRS feed

  • Question

  • code snippet:


     

    public List<Page> getPages()
            {
                using (MultilogueClassesDataContext multilogueContext = new MultilogueClassesDataContext())
                {
                    return multilogueContext.Pages.ToList();
                }
            }
    When im creating multiloguecontext it says Object reference not set to an instance of an object. I am using asp.net MVC2. What could be the problem? Pages is a table in my database which stores the names of the pages.

     

    Wednesday, March 31, 2010 12:27 PM

Answers

  • Hello michel,

    I agree with Alberto, please try to check if you add all the references for linq. Particularly in ASP.NET MVC, you may need to add directive, for example:
    using YourASPNETMVCApplication.Models;
    if you create the .dbml file in the Models folder.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Friday, April 2, 2010 9:48 AM

All replies

  • Do you have all the reference for LINQ to SQL?
    Alberto Diaz Martin twitter://@adiazcan | http://geeks.ms/blogs/adiazmartin
    Wednesday, March 31, 2010 1:49 PM
  • Hello michel,

    I agree with Alberto, please try to check if you add all the references for linq. Particularly in ASP.NET MVC, you may need to add directive, for example:
    using YourASPNETMVCApplication.Models;
    if you create the .dbml file in the Models folder.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Friday, April 2, 2010 9:48 AM
  • Hi, i have the same problem, but i have all references. When i try an example in a new solution works fine, but when i add the same things to my solution, gives the same error.  Here is the code

        public class HomeController : Controller
        {

            private IRepository<Users> _usersRepository;

            public IRepository<Users> UsersRepository
            {
                get { return _usersRepository; }
                set { _usersRepository = value; }
            }

            public ActionResult Index()
            {
               
                int page = 2;
                int rows = 3;
               
                int pageIndex = Convert.ToInt32(page) - 1;
                int pageSize = rows;
                int totalRecords = 0;
                int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
                //ideally do real paging not load all data like this.
                IList<Users> context = UsersRepository.Load(); -------------------> ERROR
                totalRecords =  context.Count;
                var users = context.Skip<Users>(pageIndex * pageSize).Take<Users>(pageSize);
               
                var jsonData = new
                {
                    total = totalPages,
                    page = page,
                    records = totalRecords,
                    rows = (
                    from User in users
                    select new
                    {   //Adrian
                        i = User.id,
                        cell = new string[] { User.id.ToString(), User.userName, User.imagesQuantity.GetValueOrDefault().ToString() }
                    }).ToArray()
                };

                ViewData["Message"] = jsonData.rows[0].cell[0].ToString();
               
                //ViewData["Message"] = "Adrian";
                return View();
            }

     

     

    I use this tutorial :

    http://netindonesia.net/blogs/junaidi/archive/2009/05/22/integrating-asp-net-mvc-spring-net-and-nhibernate.aspx

    Wednesday, May 12, 2010 6:36 PM