0

Be gentle, its only an illusion that it looks like a duplicate, but different circumstances ;)

I have a solution with three separate projects, the main project and a BLL and a DAL project (n-tier). In the DAL I have generated Linq To SQL classes from an existing database, as well as a class that has all the crud operations, and another class for custom data classes. The main project references the BLL and the BLL references the DAL.

In the main project I am populating a grid from a method in the BLL that calls the method from the DAL and that returns a list. I have stepped through the code and I keep getting the error.

Object reference not set to an instance of an object.

and it brings me to this method in my designer of the Linq To SQL classes (.dbml)

public BasicInventoryDBDataContext() : 
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["BasicInventoryConnectionString"].ConnectionString, mappingSource)

When I try and databind to a grid. I have looked in my webconfig for the DAL project and it seems legit because the connection string is in there.

I have looked around on SO and have seen similar questions to this error, but none pertaining (that I have found) when the DAL is in a separate project, let alone when using Linq To SQL.

I have tried, deleting the DAL and BLL projects and recreating them ( Not a big deal since, I have just started the project, and its in its infancy ). I haven't had this issue when I have the Linq To SQL classes in the same project. Any idea's?

EDIT Here is the page I am using to make sure things are working before continuing.

#region Initialization

public BasicInventoryDAL bl = new BasicInventoryDAL();

#endregion

protected void Page_Load(object sender, EventArgs e)
{
    rgVendor.DataSource = bl.GetVendorBrowse();
    rgVendor.DataBind();
}

I have put a break of initialization of BasicInventoryDAL, and when I step through, its brings me to the DAL Project

 public class BasicInventoryDAL
 {
    #region Initialization

    public BasicInventoryDBDataContext dc = new BasicInventoryDBDataContext();

    #endregion

    public List<VendorBrowse> GetVendorBrowse()
    {
        List<VendorBrowse> lst = new List<VendorBrowse>();

        var queryGetVendorBrowse = from f in dc.vw_Vendor_Browses
                                   select f;

        foreach (var v in queryGetVendorBrowse)
            lst.Add(new VendorBrowse
            {
                VendorID = v.VendorID,
                VendorName = v.VendorName,
                City = v.City,
                Country = v.Country
            });

        return lst;
    }
}

As soon as it gets to the public BasicInventoryDBDataContext dc = new BasicInventoryDBDataContext(); That's when the error gets thrown.

Here is the connection string in my web config

<connectionStrings>
    <add name="BasicInventoryConnectionString" connectionString="Data Source=HERBERT\SQLEXPRESS;Initial Catalog=BasicInventory;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Chris
  • 2,613
  • 7
  • 34
  • 93
  • If you don't want the question to be duplicate you need to provide actionable information. So far your post is very close to "error happened and I tried alot". There need to be way more details for someone unfamiliar with your code to suggest some concrete steps to solve it. – Alexei Levenkov Jan 21 '15 at 04:20
  • @AlexeiLevenkov, I see what you are saying. Its also looks pretty vague with out the code and steps. I'll make an edit and show everything and add more detail. – Chris Jan 21 '15 at 04:21
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 21 '15 at 04:24
  • `global::System.Configuration.ConfigurationManager.ConnectionStrings["BasicInventoryConnectionString"]` is `null`. – John Saunders Jan 21 '15 at 04:25
  • @JohnSaunders, I don't understand how it can be null. Its generated by the Linq To SQL, the connectionstring in the webconfig is named properly. I'm just about to add more code to my question. – Chris Jan 21 '15 at 04:29
  • @JohnSaunders, just looking over your link now... – Chris Jan 21 '15 at 04:35
  • @JohnSaunders, I think you may have jumped the gun, because even though the link talks about null references and explains the causes for it, but it doesn't discuss when the null is at an auto generated string by the Linq to SQL designer, or anything that remotely touches it. You pointed out that the string is NULL, which is what the error is pointing at. A point in my question "I don't have this error when the Linq To SQL is not in a separate project". – Chris Jan 21 '15 at 04:52
  • 1
    Doesn't matter who generates the code - the cause is the same. Especially in this case because it's also a question of whether the correct value is in the correct config file. – John Saunders Jan 21 '15 at 06:12
  • @JohnSaunders, in your last comment, helped me figure out what the issue was, I didn't realize that each of the projects webconfig files needed to have the connectionstring and just not lying in the DAL. I don't understand why, but after adding the connectionstring to the other webconfigs makes it work. – Chris Jan 21 '15 at 13:34
  • This question should be reopened based on the fact that there was more to it than just a null reference, and so I can give an answer to the question. – Chris Jan 21 '15 at 13:41
  • No, it's _still_ the exact same problem. How do you think I knew the connection string wasn't working? By knowing it's always the same problem and then by looking to see what opportunity you gave for the problem to occur. Config file values, either `ConnectionStrings` or `AppSettings`, are notorious causes of `NullReferenceException`. I knew this was your problem because the generated code _assumed_ that `ConnectionStrings["BasicInventoryConnectionString"]` was non-null. Bad assumption. If you feel it's necessary, then add a new answer to the linked dupe. – John Saunders Jan 21 '15 at 16:13
  • 1
    @Chris: there is _nothing_ more than a NullReferenceException caused by a piece of code that assumes the reference can never be null. It just happens that the code making the bad assumption was generated code and that the reason the reference was null is that the connection string was in the wrong config file. – John Saunders Jan 22 '15 at 13:59

1 Answers1

0

The NullReferenceException was thrown because the connection string was defined in the wrong config file. The generated code assumed that the connection string was available. That was a bad assumption, even if it was Microsoft making the bad assumption.

Since Day 1, .NET configuration has always been in the config file associated with the executable code. In the case of ASP.NET, it needs to be in the web.config files associated with the web application. In the case of a .EXE, it has to be in programname.exe.config.

John Saunders
  • 157,405
  • 24
  • 229
  • 388