Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
Glad to back with get suggestion from u. Let me tell you, I tried out following code

Private Sub createDCList(ByVal objDT As DataTable)
        Dim row As DataRow
        Dim intIndex As Integer = 0
        Dim pnlCheckList As New Panel
        pnlCheckList.Direction = ContentDirection.LeftToRight   'POSITIONING OF CONTROLS IN PANEL
        pnlCheckList = Page.FindControl("pnlJCList")
        For Each row In objDT.Rows
            Dim lblHeading As New Label
            lblHeading.Text = row.Item("CheckListName").ToString() & ":"
            lblHeading.ID = "lblHeading_" & intIndex
            pnlCheckList.Controls.Add(lblHeading)          'Line Error
            intIndex = intIndex + 1
        Next
    End Sub


I was getting error on line Error saying:
Object reference nto set to an instance of an object

As per my knowledge inside FOR LOOP, pnlCheckList is not available
Any idea how to refer it?

Thanks in advance
Posted

If pnlJCList exists in the Page you use that object directly. Why are using FindControl in the first place.

This should work:

VB
Private Sub createDCList(ByVal objDT As DataTable)
        Dim row As DataRow
        Dim intIndex As Integer = 0
        For Each row In objDT.Rows
            Dim lblHeading As New Label
            lblHeading.Text = row.Item("CheckListName").ToString() & ":"
            lblHeading.ID = "lblHeading_" & intIndex
            pnlJCList.Controls.Add(lblHeading)          'Line Error
            intIndex = intIndex + 1
        Next
End Sub


Mark it as answer if it is helpful
 
Share this answer
 
v3
Comments
Eduard Keilholz 8-Apr-11 6:37am    
sharp, +5
Venkatesh Mookkan 8-Apr-11 6:47am    
Thanks.
dhage.prashant01 8-Apr-11 6:43am    
so i should find panel in for loop?
Venkatesh Mookkan 8-Apr-11 6:48am    
Use pnlJCList object directly. No need to use FindControl.
dhage.prashant01 8-Apr-11 7:13am    
great help. . .
If you look at the MSDN[^] documentation for Page.FindControl, then you will find:
Return Value
Type: System.Web.UI.Control
The specified control, or null if the specified control does not exist.

Check your names!
 
Share this answer
 
The answer is simple, if the panel doen't exist at the time you run the code, you cannot access it. You're going to have to move the moment of running this code to a cetrain moment where the panel does exist.
 
Share this answer
 
why are you doing "Dim pnlCheckList As New Panel" and not putting panel control on page and then do whatever you want?
 
Share this answer
 
Comments
web works 8-Apr-11 13:50pm    
and if it is yr requirement then pnlCheckList has to be available on page to access!!

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