Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object 1

Status
Not open for further replies.

KDavie

Programmer
Joined
Feb 10, 2004
Messages
441
Location
US
I have a datagrid with a checkbox in it. I am trying to loop through the grid so I can check if the checkbox is checked. My Code:

Code:
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox
Dim intCorrespondenceID As Integer
For Each dgItem In dgReadyForLetter.Items
     chkSelected = CType(dgReadyForLetter.FindControl("chkSelection"), CheckBox)
     [b]If chkSelected.Checked Then[/b]
        'do some stuff
     End If
Next

The bold text denotes where the error is being thrown from. In the previous line of code I am trying to set chkSelected to the checkbox control which has an ID of "chkSelection." Can anyone tell me what I am doing wrong?

Any help is appreciated.

-Kevin
 
Try chaning this line:
Code:
chkSelected = CType(dgReadyForLetter.FindControl("chkSelection"), CheckBox)
To this:
Code:
chkSelected = CType([b]dgItem[/b].FindControl("chkSelection"), CheckBox)

You can also use the ItemDataBound event of the grid in the same way.

Jim
 
or,

chkSelected = dgItem.FindControl("chkSelection")

 
Duh!!!!!

Thanks for the quick response, your eyes saw immediately what I've been looking for for 30 minutes now!

Thanks again.

-Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top