I have a N-Tier ASP.NET 4.8 WebForms app.
I am attempting to: 1) Call a function at the Business Layer, which will then 2) Call a function at the Data Layer which will then 3) Query the database results into a DataTable and then 4) Return that DataTable to the Business Layer and then 5) return the same DataTable to the Presentation Layer.
When I call the Business Layer function directly from the Presentation Layer, I get a Null Reference Error "Object reference not set to an instance of an object."
This exception occurs at the Business Layer "Return RewardsDataAccessContext.GetRewardTypes()"
Presentation Layer
Dim RewardsContext As New Library.Rewards
dgTest.DataSource = RewardsContext.GetRewardTypes()
dgTest.DataBind()
Business Layer
Public Class Rewards
Public Function GetRewardTypes() As DataTable
Dim RewardsDataAccessContext As DataAccess.Rewards
Return RewardsDataAccessContext.GetRewardTypes()
End Function
End Class
Data Layer
Protected dtReturn As DataTable = New DataTable()
Public Function GetRewardTypes() As DataTable
Using conn = New SqlConnection(ConfigurationManager.ConnectionStrings("csName").ConnectionString)
Using cmd = New SqlCommand("spName", conn)
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
dtReturn.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection))
Return dtReturn
End Using
End Using
End Function
If I call the Data Layer function directly from the Presentation Layer, I get the expect DataTable returned in-tact. Only when it passes through the Business Layer (Library) do I get an exception.
Dim RewardsDataAccessContext As New DataAccess.Rewards
dgTest.DataSource = RewardsDataAccessContext.GetRewardTypes()
dgTest.DataBind()