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>
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.