Solved

Object reference not set to an instance of an object.

Posted on 2006-11-13
17
323 Views
Last Modified: 2008-02-01
Working with IE Web Controls for the first time and I'm having a hard time.  I am using the code from the following URL:

http://www.aspnet101.com/aspnet101/tutorials.aspx?id=28

but I moved the actual code to the code behind page.

Here's the error:

Object reference not set to an instance of an object.

Source Error:

Line 51:             pnl = CType(FindControl("pnl" & i.ToString), Panel)
Line 52:             If mytabstrip.SelectedIndex = i Then
Line 53:                 pnl.Visible = "True"    ******this is where it's bombing out
Line 54:             Else
Line 55:                 pnl.Visible = "False"
 


Here is the full subroutine

    Sub doChange()
        Dim i As Integer
        Dim pnl As Panel
        'Makes each Panel Visible or Invisible, depdending on which Tab is clicked.
        For i = 0 To 4
            Dim s As String
            s = i.ToString
            pnl = CType(FindControl("pnl" & i.ToString), Panel)
            If mytabstrip.SelectedIndex = i Then
                pnl.Visible = "True"
            Else
                pnl.Visible = "False"
            End If
        Next
    End Sub


and here's the panel tag

<asp:panel ID="pnl0" Height="100" BorderWidth="2"  Visible="False" Runat="server">

I know this is probably kind of elementary but I'm stumped.


0
Comment
Question by:stretch73
  • 4
  • 4
  • 3
  • +4
17 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 17931229
Hi stretch73,
i think that the problem is that pnl1, 2, 3, 4 not exsist.
may be you have to add an additional line

pnl = CType(FindControl("pnl" & i.ToString), Panel)
if not pnl is nothing then
If mytabstrip.SelectedIndex = i Then
    pnl.Visible = "True"
Else
    pnl.Visible = "False"
End If
end if

Cheers!
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17931232
Not sure, but has the panel control been created at that point ?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17931235
also use

 pnl.Visible = True

instead of

 pnl.Visible = "True"
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17931238
since pnl.Visible is boolean and not a string
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17931251
Basically the line pnl = CType(FindControl("pnl" & i.ToString), Panel) is failing to locate a control with the id of "pnl". I'm not sure why this happens when you move the code to a code behind file (I assume you've properly referenced the code behind file).

I'm also not sure how this value of i is being set. I assume you're incrementing through the list of tabs and you've namned each tab pnl0, pnl1....
0
 
LVL 6

Expert Comment

by:bigphuckinglizard
ID: 17931275
you need to declare the panel at the top of your code e.g.

Imports System
...

Protected WithEvents pnl as Panel
 ...

   Sub doChange()
        Dim i As Integer
        Dim pnl As Panel
        'Makes each Panel Visible or Invisible, depdending on which Tab is clicked.
        For i = 0 To 4
            Dim s As String
            s = i.ToString
            pnl = CType(FindControl("pnl" & i.ToString), Panel)
            If mytabstrip.SelectedIndex = i Then
                pnl.Visible = true
            Else
                pnl.Visible = false
            End If
        Next
    End Sub
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17931280
mmarinov is correct and you should add the check for Nothing. However, the real question is why does it not exist?

YZlat is also correct but this i snot causing your problem. Infact I beleive that this will implicitly case to true anyway.

0
 
LVL 6

Expert Comment

by:bigphuckinglizard
ID: 17931328
just looking at your code, should be a line for each panel e.g.

Protected WithEvents pnl0 as Panel
Protected WithEvents pnl1 as Panel
Protected WithEvents pnl2 as Panel
Protected WithEvents pnl3 as Panel
Protected WithEvents pnl4 as Panel
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:stretch73
ID: 17931352
Wow, great responses.  

Okay the is nothing check got it to actually run but now it's just showing the text for the panels and not any actual panel control.  I had a rough time getting these things installed because the readme file is a little disjointed.  
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17931362
You only need the delcarations that  bigphuckinglizard proposes if you are uinf 1.1. In 2.0 they are automatically added to the partial class.
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17931431
FindContol can also fail if you are not calling it on the parent container. Many controls in ASP.Net are "Naming Containers" and each one of these has a collection of controls. Each one of these controlc can also be a "Naming Container" and so on... When you call FindControl on a Naming Container, it only searches that Naming Container's immediate controls, not the controls of thiose controls.

Take a look at the source code in the browser and you will see that your pnl0 control has a name like ctl100$ctl101$pnl0.

I've not used this control before but my guess is you'll have to do this...

            pnl = CType(mytabstrip.FindControl("pnl" & i.ToString), Panel)
0
 
LVL 6

Expert Comment

by:bigphuckinglizard
ID: 17931464
maybe posting the full source for both files (and what version of asp.net you're using) would help?
0
 

Author Comment

by:stretch73
ID: 17932150
Okay, here are the full source files and I'm using 2.0.50727

categorySelect.aspx

**********************************************************************************************************************************
<%@ Page Language="VB" MasterPageFile="~/Mobility.master" AutoEventWireup="false" CodeFile="categorySelect.aspx.vb" Inherits="categorySelect" %>
<%@ Register TagPrefix="aspnet" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>

<asp:Content ID="Content1" contentplaceholderid="PageContent" runat="Server">

<table border="0" cellpadding="0" cellspacing="0" width="600">
      <tr>
            <td align="Left" valign="Top">
            <aspnet:tabstrip id="mytabstrip"
                  onSelectedIndexChange="IndexChange"
                  runat="server"
                  Width="338px"
                  Height="18px"
                  Font-Size="8pt"
                  AutoPostBack="True"
                  TabSelectedStyle="background-color:#ffffff;color:#000000;"
                  TabHoverStyle="background-color:#E0E0E0;color:#000000;"
                  TabDefaultStyle="background-color:#C0C0C0;
                              font-family:verdana;
                              border-style:solid;
                              border-width:1px;
                              border-color:black;
                              font-weight:bold;
                              color:#ffffff;
                              text-align:center;">
                     <aspnet:Tab Text="Step 1"></aspnet:Tab>
                     <aspnet:Tab Text="Step 2"></aspnet:Tab>
                     <aspnet:Tab Text="Step 3"></aspnet:Tab>

                  <aspnet:Tab Text="Step 5"></aspnet:Tab>
            </aspnet:tabstrip></td>
      </tr>
            <tr>
            <td>&nbsp;</td>
      </tr>
      <tr>
            <td>      
      

    <asp:panel ID="pnl0" Height="100" BorderWidth="2" Width="100%"  Visible="False" Runat="server">

 
    <asp:DataGrid id="dgrdRecords"
        onEditCommand="dgrdRecords_EditCommand"
        onUpdateCommand="dgrdRecords_UpdateCommand"
        OnCancelCommand="dgrdRecords_CancelCommand"
        DataKeyField="MobileNumber"
        AutoGenerateColumns="false"
        cellpadding="2"
        cellspacing="1"
        Width="800"
        runat="server"
        GridLines="Both"
        EditItemStyle-BackColor="#FFFFCC"
        >
   
    <HeaderStyle BackColor="#CECECE" Font-Size="Large"/>      
    <AlternatingItemStyle BackColor="#EEEEEE" />

    <Columns>
        <asp:TemplateColumn HeaderText="Person Number" HeaderStyle-CssClass="header_row" SortExpression="Name">
            <ItemTemplate>
              <%# DataBinder.Eval(Container.DataItem, "PersonNumber") %>&nbsp;
            </ItemTemplate>
           
            <EditItemTemplate>
              <asp:TextBox runat="server"
                 id="txtPersonNumber"
                 Columns="8"
                 Font-Size="9"
                 MaxLength="8"
                 Text='<%# DataBinder.Eval(Container.DataItem, "PersonNumber") %>' />
                 
                <asp:CustomValidator
                 ID="PersonNumberValidator"
                 ControlToValidate="txtPersonNumber"
                 Display="Dynamic"
                 runat="server"
                 ErrorMessage="Person Number must be an eight digit numeric entry."
                 OnServerValidate="PersonNumberValidator_Validate" />
                 
            </EditItemTemplate>
        </asp:TemplateColumn>
           </Columns>
   
    </asp:DataGrid>
</asp:panel>
</td>
</tr>
</table>
</asp:Content>
**********************************************************************************************************************************



categorySelect.aspx.vb
**********************************************************************************************************************************
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Drawing

Partial Class categorySelect

    Inherits System.Web.UI.Page

    Private mVendor As String
    Private mAction As String

    Private oConn As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))

    Sub IndexChange(ByVal sender As Object, ByVal e As EventArgs)
        doChange()
    End Sub

    Sub doChange()
        Dim i As Integer
        Dim pnl As Panel

        'Makes each Panel Visible or Invisible, depdending on which Tab is clicked.
        For i = 0 To 4
            Dim s As String
            s = i.ToString
            If Not pnl Is Nothing Then
                pnl = CType(FindControl("pnl" & i.ToString), Panel)
                If mytabstrip.SelectedIndex = i Then
                    pnl.Visible = True
                Else
                    pnl.Visible = False
                End If
            End If
        Next
    End Sub
End Class
**********************************************************************************************************************************

0
 

Author Comment

by:stretch73
ID: 17938436
On doing some research, I realized Web Controls aren't even supported any more.  The MultiView seems like a much more efficient way to accomplish this.
0
 
LVL 6

Expert Comment

by:bigphuckinglizard
ID: 17938471
the problem seems to be that you only have a pnl0, there's no pnls1-4 to find in the loop, i assume they're in progress?

add the following dummy panels in somewhere and you should be ok:

</asp:panel> <-- end of definition for pnl0
<asp:panel ID="pnl1" Height="100" BorderWidth="2" Width="100%"  Visible="False" Runat="server">Temp 1</asp:Panel>
<asp:panel ID="pnl2" Height="100" BorderWidth="2" Width="100%"  Visible="False" Runat="server">Temp 2</asp:Panel>
<asp:panel ID="pnl3" Height="100" BorderWidth="2" Width="100%"  Visible="False" Runat="server">Temp 3</asp:Panel>
<asp:panel ID="pnl4" Height="100" BorderWidth="2" Width="100%"  Visible="False" Runat="server">Temp 4</asp:Panel>
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17976595
Closed, 150 points refunded.
DarthMod
Community Support Moderator
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now