Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a dynamic checkbox at Page Load (Not isPostback)

Now if a new data is to be inserted, the system should check first whether the data is already exist in the created checkboxlist.

i have tried this, but error occured
Object reference not set to an instance of an object.


This is my code.

VB
Dim row As DataRow = dt.NewRow()

  Dim chlist As CheckBoxList = DirectCast(FindControl("cbTest"), CheckBoxList)
      For i = 0 To dtSH.Rows.Count - 1

        For Each Listdata As ListItem In chlist.Items

            For Each row1 As DataRow In dt.Rows

               If dtSH.Rows(i)("EMPID_T") <> row1("EMPID_T").ToString Then
                             row = dt.NewRow()
                             row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
                             row("NAME_T") = dtSH.Rows(i)("NAME_T")
                             dt.Rows.Add(row)
                         End If

                     Next
                 Next

             Next


i dont know whether here
VB
Dim chlist As CheckBoxList = DirectCast(FindControl("cbTest"), CheckBoxList)
or here
VB
For Each Listdata As ListItem In chlist.Items

that goes wrong. A little help please. TQ!
Posted
Updated 26-Jul-12 22:53pm
v2

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
 
Share this answer
 
where is the declaration of i?



mark as answer if it helped you. :)
 
Share this answer
 
Comments
snamyna 27-Jul-12 5:29am    
I just paste some piece of my code.
The error points here "For Each Listdata As ListItem In chlist.Items"
graciax8 27-Jul-12 6:03am    
it is right.
snamyna 27-Jul-12 6:15am    
I think the error source is here
"Dim chlist As CheckBoxList = DirectCast(FindControl("cbTest"), CheckBoxList)"
because when I debug, chlist is nothing.
How is it actually to pass cbTest properties and datatable into chlist?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900