Why does the code below have the error in the subject of my post? I don't get it.... How do I make it go away. Any help is appreciated

Partial Class NIR_Approvals_NIA

Inherits System.Web.UI.Page


Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

Dim unitCost As Decimal = CDec(CType(DetailsView1.FindControl("Label4"), Label).Text)

Dim annualSqty As Decimal = CDec(CType(DetailsView1.FindControl("Label5"), Label).Text)

Dim annualSales As Decimal = CDec(CType(DetailsView1.FindControl("Label6"), Label).Text)

MathFormulas(unitCost, annualSqty, annualSales)

NIAUPD.Update()

End Sub

Private Sub MathFormulas(ByVal unitCost As Decimal, ByVal annualSqty As Decimal, ByVal annualSales As Decimal)

Dim costReturn As Decimal = CDec(CType(DetailsView1.FindControl("Label1"), Label).Text)

Dim profitReturn As Decimal = CDec(CType(DetailsView1.FindControl("Label2"), Label).Text)

Dim totalReturn As Decimal = CDec(CType(DetailsView1.FindControl("Label3"), Label).Text)

costReturn = unitCost * annualSqty

profitReturn = annualSales - costReturn

totalReturn = profitReturn / annualSales

End Sub

End Class

The main line of code throwing the exception is Dim unitCost As Decimal = CDec(CType(DetailsView1.FindControl("Label4"), Label).Text)

I'm more of a C# guy than VB so you'll have to do the coding on your end but:

Dim unitCost As Decimal = CDec(CType(DetailsView1.FindControl("Label4"), Label).Text)

This likely means this section of the code is failing:

DetailsView1.FindControl("Label4")

Try something like:

Dim aLabel As Label = CType(DetailsView1.FindControl("Label4"), Label)

Set a debug point and if aLabel is null then that explains your null reference exception meaning that Label4 could not be found in the DetailView container.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.