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

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
 
C

Chris, Master of All Things Insignificant

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
 
U

UGH

That did not work. The st is never empy. I think somthig is wrong with dp
variable.
 
C

Chris Dunaway

You should use similar code to what Chris posted to check the dp
variable as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top