Object reference not set to an instance of an object :(

  • Thread starter Thread starter UGH
  • Start date Start date
U

UGH

I am looping the grid rows to set the values in the dropdownlists. I keep
getting a run time error "Object reference not set to an instance of an
object." The error occurs on line where it says liitem=
dp.items.findbyvalues(st). I am not sure what I am doing wrong. I included
my codes so someone can point out my mistake. Thanks.


Private Sub reset_values()
Dim i As Long
Dim dp As DropDownList

Dim intIndex As Int32
Dim liItem As ListItem

Dim st As String



For i = 0 To grdAct.Items.Count - 1
dp = CType(grdAct.FindControl("dpl"), DropDownList)
st = grdAct.Items(i).Cells(5).Text



liItem = dp.Items.FindByValue(st) ( error occurs on this line )
intIndex = dp.Items.IndexOf(liItem)
dp.SelectedIndex = intIndex
Next

End Sub
 
I think the following code will solve your issue. Why st is returning
"nothing" I can not help you with.

Private Sub reset_values()
Dim i As Long
Dim dp As DropDownList

Dim intIndex As Int32
Dim liItem As ListItem

Dim st As String

For i = 0 To grdAct.Items.Count - 1
dp = CType(grdAct.FindControl("dpl"), DropDownList)
st = grdAct.Items(i).Cells(5).Text

if not st is nothing then
'st is value, go ahead and do it
liItem = dp.Items.FindByValue(st) ( error occurs on this
line )
intIndex = dp.Items.IndexOf(liItem)
dp.SelectedIndex = intIndex
else
'For some reason grdAct.Items(i).Cells(5).Text
'returns nothing
end if
Next
 
You should use similar code to what Chris posted to check the dp
variable as well.
 
Back
Top