code behind issues Run time errors help!

Discussion in 'ASP .Net' started by =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Jan 25, 2005.

  1. Still new to code behind so i'm not sure what the problem is (i'm using
    visualStudio.net for the first time) like my last question i'll bet this has
    a quick answer. thanks
    kes
    here is the error: and please for give the excessive text

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information about
    the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.

    Source Error:
    Line 56: conTyp.Open()
    Line 57: dtrTyp = cmdSelectTyp.ExecuteReader()
    Line 58: dGrdTyp.DataSource = dtrTyp
    Line 59: dGrdTyp.DataBind()
    Line 60: dtrTyp.Close()

    Source File: E:\WebSites\DamarStone\damarStone\typ.aspx.vb Line: 58
    Stack Trace:
    [NullReferenceException: Object reference not set to an instance of an
    object.]
    damarStone.WebForm1.BindDataGrid() in
    E:\WebSites\DamarStone\damarStone\typ.aspx.vb:58
    damarStone.WebForm1.Page_Load(Object sender, EventArgs e) in
    E:\WebSites\DamarStone\damarStone\typ.aspx.vb:35
    System.Web.UI.Control.OnLoad(EventArgs e) +67
    System.Web.UI.Control.LoadRecursive() +35
    System.Web.UI.Page.ProcessRequestMain() +750
    ********
    and the pages:
    ********
    typ.aspx.vb:
    Imports System.Data.OleDb
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls
    Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents dGrdTyp As System.Web.UI.WebControls.DataGrid
    Public thsSitePath As String
    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents lblUser As System.Web.UI.WebControls.Label
    Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lnkToHome As System.Web.UI.WebControls.LinkButton

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Call Get_location()
    Call BindDataGrid()
    End Sub
    Private Sub Get_location()
    Dim thsUri As Uri
    Dim thsHost As String
    thsUri = Request.Url
    thsHost = thsUri.Host
    If (thsHost = "www.damarstone.com") Then
    thsSitePath = "c:\websites\damar"
    Else
    thsSitePath = "e:\websites\damarstone\"
    End If
    lblUser.Text = thsSitePath
    End Sub
    Private Sub BindDataGrid()
    Dim conTyp As OleDbConnection
    Dim cmdSelectTyp As OleDbCommand
    Dim dtrTyp As OleDbDataReader
    lnkToHome.Text = "me"
    conTyp = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
    Source=" & thsSitePath & "db\damarstone.mdb")
    cmdSelectTyp = New OleDbCommand("SELECT t.typID, t.typName,
    t.typeDesc FROM typ t ORDER BY t.typName;", conTyp)
    conTyp.Open()
    dtrTyp = cmdSelectTyp.ExecuteReader()
    dGrdTyp.DataSource = dtrTyp
    dGrdTyp.DataBind()
    dtrTyp.Close()
    conTyp.Close()
    End Sub
    End Class
    ***********
    typ.aspx:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="typ.aspx.vb"
    Inherits="damarStone.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</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 MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Label id="lblUser" runat="server" Font-Size="X-Small">Current
    Action</asp:Label><br>
    <asp:Label id="lblStatus" runat="server" Font-Size="XX-Small"
    Width="240px">On the main stone types page</asp:Label><br>
    <asp:LinkButton id="lnkToHome" runat="server">Home</asp:LinkButton><br>
    <asp:DataGrid id="DataGrid1" style="" runat="server" >
    <Columns>
    <asp:HyperLinkColumn DataNavigateUrlField="typID"
    DataNavigateUrlFormatString="Slabs.aspx?TypID={0}"
    DataTextField="TypName" HeaderText="Select Stone" runat="server" />
    </Columns>
    </asp:DataGrid></form>
    </body>
    </HTML>
     
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Jan 25, 2005
    #1
    1. Advertisements

  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    Marina Guest

    dGrdTyp is a variable set to null. It has never been instantiated.

    Typically this happens in asp.net when you have declared the variable in
    your code behind page, but no corresponding element exists in the .aspx.

    I see in your .aspx your grid is call DataGrid1, not dGrdType. So there you
    go.

    "Kurt Schroeder" <> wrote in message
    news:...
    > Still new to code behind so i'm not sure what the problem is (i'm using
    > visualStudio.net for the first time) like my last question i'll bet this
    > has
    > a quick answer. thanks
    > kes
    > here is the error: and please for give the excessive text
    >
    > Object reference not set to an instance of an object.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about
    > the error and where it originated in the code.
    >
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    >
    > Source Error:
    > Line 56: conTyp.Open()
    > Line 57: dtrTyp = cmdSelectTyp.ExecuteReader()
    > Line 58: dGrdTyp.DataSource = dtrTyp
    > Line 59: dGrdTyp.DataBind()
    > Line 60: dtrTyp.Close()
    >
    > Source File: E:\WebSites\DamarStone\damarStone\typ.aspx.vb Line: 58
    > Stack Trace:
    > [NullReferenceException: Object reference not set to an instance of an
    > object.]
    > damarStone.WebForm1.BindDataGrid() in
    > E:\WebSites\DamarStone\damarStone\typ.aspx.vb:58
    > damarStone.WebForm1.Page_Load(Object sender, EventArgs e) in
    > E:\WebSites\DamarStone\damarStone\typ.aspx.vb:35
    > System.Web.UI.Control.OnLoad(EventArgs e) +67
    > System.Web.UI.Control.LoadRecursive() +35
    > System.Web.UI.Page.ProcessRequestMain() +750
    > ********
    > and the pages:
    > ********
    > typ.aspx.vb:
    > Imports System.Data.OleDb
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    > Imports System.Web.UI.HtmlControls
    > Public Class WebForm1
    > Inherits System.Web.UI.Page
    > Protected WithEvents dGrdTyp As System.Web.UI.WebControls.DataGrid
    > Public thsSitePath As String
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    > Protected WithEvents lblUser As System.Web.UI.WebControls.Label
    > Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
    > Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    > Protected WithEvents lnkToHome As System.Web.UI.WebControls.LinkButton
    >
    > 'NOTE: The following placeholder declaration is required by the Web
    > Form
    > Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > Call Get_location()
    > Call BindDataGrid()
    > End Sub
    > Private Sub Get_location()
    > Dim thsUri As Uri
    > Dim thsHost As String
    > thsUri = Request.Url
    > thsHost = thsUri.Host
    > If (thsHost = "www.damarstone.com") Then
    > thsSitePath = "c:\websites\damar"
    > Else
    > thsSitePath = "e:\websites\damarstone\"
    > End If
    > lblUser.Text = thsSitePath
    > End Sub
    > Private Sub BindDataGrid()
    > Dim conTyp As OleDbConnection
    > Dim cmdSelectTyp As OleDbCommand
    > Dim dtrTyp As OleDbDataReader
    > lnkToHome.Text = "me"
    > conTyp = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
    > Source=" & thsSitePath & "db\damarstone.mdb")
    > cmdSelectTyp = New OleDbCommand("SELECT t.typID, t.typName,
    > t.typeDesc FROM typ t ORDER BY t.typName;", conTyp)
    > conTyp.Open()
    > dtrTyp = cmdSelectTyp.ExecuteReader()
    > dGrdTyp.DataSource = dtrTyp
    > dGrdTyp.DataBind()
    > dtrTyp.Close()
    > conTyp.Close()
    > End Sub
    > End Class
    > ***********
    > typ.aspx:
    > <%@ Page Language="vb" AutoEventWireup="false" Codebehind="typ.aspx.vb"
    > Inherits="damarStone.WebForm1"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>WebForm1</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 MS_POSITIONING="GridLayout">
    > <form id="Form1" method="post" runat="server">
    > <asp:Label id="lblUser" runat="server" Font-Size="X-Small">Current
    > Action</asp:Label><br>
    > <asp:Label id="lblStatus" runat="server" Font-Size="XX-Small"
    > Width="240px">On the main stone types page</asp:Label><br>
    > <asp:LinkButton id="lnkToHome" runat="server">Home</asp:LinkButton><br>
    > <asp:DataGrid id="DataGrid1" style="" runat="server" >
    > <Columns>
    > <asp:HyperLinkColumn DataNavigateUrlField="typID"
    > DataNavigateUrlFormatString="Slabs.aspx?TypID={0}"
    > DataTextField="TypName" HeaderText="Select Stone" runat="server" />
    > </Columns>
    > </asp:DataGrid></form>
    > </body>
    > </HTML>
    >
    >
    >
    >
    >
     
    Marina, Jan 25, 2005
    #2
    1. Advertisements

  3. I can't believe this!!!!
    thanls
    OU1!!!
    kes

    "Marina" wrote:

    > dGrdTyp is a variable set to null. It has never been instantiated.
    >
    > Typically this happens in asp.net when you have declared the variable in
    > your code behind page, but no corresponding element exists in the .aspx.
    >
    > I see in your .aspx your grid is call DataGrid1, not dGrdType. So there you
    > go.
    >
    > "Kurt Schroeder" <> wrote in message
    > news:...
    > > Still new to code behind so i'm not sure what the problem is (i'm using
    > > visualStudio.net for the first time) like my last question i'll bet this
    > > has
    > > a quick answer. thanks
    > > kes
    > > here is the error: and please for give the excessive text
    > >
    > > Object reference not set to an instance of an object.
    > > Description: An unhandled exception occurred during the execution of the
    > > current web request. Please review the stack trace for more information
    > > about
    > > the error and where it originated in the code.
    > >
    > > Exception Details: System.NullReferenceException: Object reference not set
    > > to an instance of an object.
    > >
    > > Source Error:
    > > Line 56: conTyp.Open()
    > > Line 57: dtrTyp = cmdSelectTyp.ExecuteReader()
    > > Line 58: dGrdTyp.DataSource = dtrTyp
    > > Line 59: dGrdTyp.DataBind()
    > > Line 60: dtrTyp.Close()
    > >
    > > Source File: E:\WebSites\DamarStone\damarStone\typ.aspx.vb Line: 58
    > > Stack Trace:
    > > [NullReferenceException: Object reference not set to an instance of an
    > > object.]
    > > damarStone.WebForm1.BindDataGrid() in
    > > E:\WebSites\DamarStone\damarStone\typ.aspx.vb:58
    > > damarStone.WebForm1.Page_Load(Object sender, EventArgs e) in
    > > E:\WebSites\DamarStone\damarStone\typ.aspx.vb:35
    > > System.Web.UI.Control.OnLoad(EventArgs e) +67
    > > System.Web.UI.Control.LoadRecursive() +35
    > > System.Web.UI.Page.ProcessRequestMain() +750
    > > ********
    > > and the pages:
    > > ********
    > > typ.aspx.vb:
    > > Imports System.Data.OleDb
    > > Imports System.Web.UI
    > > Imports System.Web.UI.WebControls
    > > Imports System.Web.UI.HtmlControls
    > > Public Class WebForm1
    > > Inherits System.Web.UI.Page
    > > Protected WithEvents dGrdTyp As System.Web.UI.WebControls.DataGrid
    > > Public thsSitePath As String
    > > #Region " Web Form Designer Generated Code "
    > >
    > > 'This call is required by the Web Form Designer.
    > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > InitializeComponent()
    > >
    > > End Sub
    > > Protected WithEvents lblUser As System.Web.UI.WebControls.Label
    > > Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
    > > Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    > > Protected WithEvents lnkToHome As System.Web.UI.WebControls.LinkButton
    > >
    > > 'NOTE: The following placeholder declaration is required by the Web
    > > Form
    > > Designer.
    > > 'Do not delete or move it.
    > > Private designerPlaceholderDeclaration As System.Object
    > >
    > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Init
    > > 'CODEGEN: This method call is required by the Web Form Designer
    > > 'Do not modify it using the code editor.
    > > InitializeComponent()
    > > End Sub
    > >
    > > #End Region
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > > 'Put user code to initialize the page here
    > > Call Get_location()
    > > Call BindDataGrid()
    > > End Sub
    > > Private Sub Get_location()
    > > Dim thsUri As Uri
    > > Dim thsHost As String
    > > thsUri = Request.Url
    > > thsHost = thsUri.Host
    > > If (thsHost = "www.damarstone.com") Then
    > > thsSitePath = "c:\websites\damar"
    > > Else
    > > thsSitePath = "e:\websites\damarstone\"
    > > End If
    > > lblUser.Text = thsSitePath
    > > End Sub
    > > Private Sub BindDataGrid()
    > > Dim conTyp As OleDbConnection
    > > Dim cmdSelectTyp As OleDbCommand
    > > Dim dtrTyp As OleDbDataReader
    > > lnkToHome.Text = "me"
    > > conTyp = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
    > > Source=" & thsSitePath & "db\damarstone.mdb")
    > > cmdSelectTyp = New OleDbCommand("SELECT t.typID, t.typName,
    > > t.typeDesc FROM typ t ORDER BY t.typName;", conTyp)
    > > conTyp.Open()
    > > dtrTyp = cmdSelectTyp.ExecuteReader()
    > > dGrdTyp.DataSource = dtrTyp
    > > dGrdTyp.DataBind()
    > > dtrTyp.Close()
    > > conTyp.Close()
    > > End Sub
    > > End Class
    > > ***********
    > > typ.aspx:
    > > <%@ Page Language="vb" AutoEventWireup="false" Codebehind="typ.aspx.vb"
    > > Inherits="damarStone.WebForm1"%>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > <HTML>
    > > <HEAD>
    > > <title>WebForm1</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 MS_POSITIONING="GridLayout">
    > > <form id="Form1" method="post" runat="server">
    > > <asp:Label id="lblUser" runat="server" Font-Size="X-Small">Current
    > > Action</asp:Label><br>
    > > <asp:Label id="lblStatus" runat="server" Font-Size="XX-Small"
    > > Width="240px">On the main stone types page</asp:Label><br>
    > > <asp:LinkButton id="lnkToHome" runat="server">Home</asp:LinkButton><br>
    > > <asp:DataGrid id="DataGrid1" style="" runat="server" >
    > > <Columns>
    > > <asp:HyperLinkColumn DataNavigateUrlField="typID"
    > > DataNavigateUrlFormatString="Slabs.aspx?TypID={0}"
    > > DataTextField="TypName" HeaderText="Select Stone" runat="server" />
    > > </Columns>
    > > </asp:DataGrid></form>
    > > </body>
    > > </HTML>
    > >
    > >
    > >
    > >
    > >

    >
    >
    >
     
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Jan 25, 2005
    #3
    1. Advertisements

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. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    745
    Alphonse Giambrone
    Jun 28, 2003
  2. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,129
    Mark Goldin
    Jan 17, 2004
  3. thomson
    Replies:
    1
    Views:
    393
    Karl Seguin [MVP]
    Jul 4, 2006
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    628
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Pierre Yves
    Replies:
    2
    Views:
    625
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page