Inexplicable (By Me) Null Reference Error

Discussion in 'ASP .Net Building Controls' started by frankvfox, May 7, 2006.

  1. frankvfox

    frankvfox Guest

    Swipe, copy and paste this into notepad for readability

    First experience with a Web User Control. Control simply an ordered
    (vertical/horizontal)
    array of text boxes that will show authorized versus actual expenses for a
    group of capital
    projects. User control enables grid alignment within a panel centered in a
    flow layout
    host page. Source code shown below for control and host. I know there are
    easier ways of
    doing this but I wanted to learn this aspect of VB.net.

    No squiggly lines presented in source editor. All references generated by
    intellisense
    dropdown during typing.

    I'm getting a null reference error within the control itself (as shown
    below). The variable
    m_Text has the appropriate value during debug. I can't figure it out. I know
    it's something
    simple but this is my first time with a web user control and don't have a
    clue. Any help will
    be really appreciated.

    Code snippets follow below.

    Frank Fox
    Hendersonville, Tennessee
    5/7/2006


    'Overview.ascx is the user control file

    Public Class Overview1
    Inherits System.Web.UI.UserControl

    Private m_Index As Integer
    Private m_Text As String

    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    'TextBox1 declared

    'all the other control declarations removed for brevity here

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'no code
    End Sub

    Public WriteOnly Property Index()
    Set(ByVal Value)
    m_Index = Value
    End Set
    End Property

    Public WriteOnly Property Text()
    Set(ByVal Value)
    m_Text = Value
    Select Case m_Index
    Case 1
    TextBox1.Text = m_Text '<---- null reference error
    announced here
    Case 2
    TextBox2.Text = m_Text
    Case 3
    TextBox3.Text = m_Text

    'Case 4 through Case 37 removed for brevity here

    Case 38
    TextBox38.Text = m_Text
    Case 39
    TextBox39.Text = m_Text
    Case 40
    TextBox40.Text = m_Text
    End Select
    End Set
    End Property

    End Class

    HTML behind:

    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="Overview.ascx.vb" Inherits="CapitalProjects.Overview1"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <DIV style="WIDTH: 624px; POSITION: relative; HEIGHT: 424px"
    ms_positioning="GridLayout">

    <asp:TextBox id="TextBox1" style="Z-INDEX: 104; LEFT: 136px; POSITION:
    absolute; TOP: 184px"
    runat="server" Width="80px" Height="20px"></asp:TextBox>
    <asp:TextBox id="TextBox2" style="Z-INDEX: 105; LEFT: 224px; POSITION:
    absolute; TOP: 184px"
    runat="server" Width="80px" Height="20px"></asp:TextBox>
    <asp:TextBox id="TextBox3" style="Z-INDEX: 106; LEFT: 312px; POSITION:
    absolute; TOP: 184px"
    runat="server" Width="80px" Height="20px"></asp:TextBox>

    'TextBox4 through TextBox37 removed for brevity here


    <asp:TextBox id="TextBox38" style="Z-INDEX: 141; LEFT: 312px; POSITION:
    absolute; TOP: 360px"
    runat="server" Width="80px" Height="20px"></asp:TextBox>
    <asp:TextBox id="TextBox39" style="Z-INDEX: 140; LEFT: 400px; POSITION:
    absolute; TOP: 360px"
    runat="server" Width="80px" Height="20px"></asp:TextBox>
    <asp:TextBox id="TextBox40" style="Z-INDEX: 139; LEFT: 488px; POSITION:
    absolute; TOP: 360px"
    runat="server" Width="80px" Height="20px"></asp:TextBox>


    __________________________________________________________________________________________________________________________________


    AllProjectsOverview.aspx.vb 'host page

    Imports System.Data.SqlClient

    Public Class AllProjectsOverview
    Inherits System.Web.UI.Page

    Dim cn As SqlConnection
    Dim cmd As SqlCommand
    Dim sdr As SqlDataReader
    Protected WithEvents ov As Overview1

    'designer generated region removed for brevity here

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim i, j, k As Integer
    Dim par1 As SqlParameter
    Dim par2 As SqlParameter
    cn = Session("Connection")
    If (cn.State.ToString = "Closed") Then cn.Open()
    For i = 1 To 7 'first
    seven rows .. eighth row is totals
    cmd = New SqlCommand("SelectAllProjectsRow", cn)
    cmd.CommandType = CommandType.StoredProcedure
    par1 = New SqlParameter("@StatusID", SqlDbType.Int)
    par2 = New SqlParameter("@CurrentYear", SqlDbType.Int)
    par1.Value = i
    par2.Value = Session("CurrentYear")
    cmd.Parameters.Add(par1)
    cmd.Parameters.Add(par2)
    sdr = cmd.ExecuteReader
    ov = New Overview1 '<-------- user control instance
    If (sdr.HasRows) Then
    sdr.Read()
    j = i + (5 * (i - 1))
    ov.Index = j '<-------- first call to user
    control .. no error
    ov.Text = sdr("auth") '<-------- second call .. error
    generated in user control
    j += 1
    ov.Index = j
    ov.Text = sdr("prior")
    j += 1
    ov.Index = j
    ov.Text = sdr("thisyr")
    j += 1
    ov.Index = j
    ov.Text = sdr("subtot")
    j += 1
    ov.Index = j
    ov.Text = sdr("remainder")
    End If
    sdr.Close()
    cmd.Dispose()
    Next
    End Sub

    End Class

    HTML behind:

    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="AllProjectsOverview.aspx.vb"
    Inherits="CapitalProjects.AllProjectsOverview"%>
    <%@ Register TagPrefix="uc1" TagName="Overview" Src="Overview.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>AllProjectsOverview</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body>
    <center>
    <form id="Form1" method="post" runat="server">
    <P>
    <uc1:Overview id="Overview1" runat="server"></uc1:Overview></P>
    <P>
    <asp:button id="btnPrint" runat="server" Text="Print" BackColor="Aqua"
    Width="150px" Font-Bold="True"
    ForeColor="Blue"></asp:button>
    <asp:label id="Label17" runat="server" Width="50px"
    ForeColor="White">x</asp:label>
    <asp:button id="btnCancel" runat="server" Text="Go Back"
    BackColor="Aqua" Width="130px" Font-Bold="True"
    ForeColor="Blue"></asp:button></P>
    </form>
    </center>
    </body>
    </HTML>
    frankvfox, May 7, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    13
    Views:
    745
    Cy Edmunds
    Jan 1, 2004
  2. Replies:
    2
    Views:
    534
  3. O.o
    Replies:
    4
    Views:
    305
    Vladimir Jovic
    Dec 11, 2009
  4. Saqib Ali
    Replies:
    6
    Views:
    1,041
    Vyacheslav Lanovets
    Oct 24, 2011
  5. Tom Cloyd
    Replies:
    0
    Views:
    107
    Tom Cloyd
    May 6, 2009
Loading...

Share This Page