Results 1 to 5 of 5

Thread: Error - Object reference not set to an instance of an object.

  1. #1
    skaswani is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    16
    Reputation
    59

    Error - Object reference not set to an instance of an object.

    Dear All,

    I am new to Asp.net and facing some issues while coding:

    i am using Visual Studio 2010 - Vb.net & Sql Server 2008

    Trying to Do :

    I have a WebForm ("frmLocations.aspx") and User Control ("cltGrids.ascx")

    User Control has two Data Grids

    i want to Bind Values to datagrid here is my code


    Code:
    Imports System.Data.SqlClient
    
    Public Class frmLocations
        Inherits System.Web.UI.Page
    
    
        Dim clsSetup As New clsSetup
        Dim clsUsers As New clsUsers
        Dim cltGrid As New cltGrids
        Dim grid1 As New DataGrid
        Dim grid2 As New DataGrid
        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim dt As New DataTable
        Dim Sql As String = ""
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            grid1 = CType(cltGrid.FindControl("GridView1"), DataGrid)
            grid2 = CType(cltGrid.FindControl("GridView2"), DataGrid)
            Call RefreshGrid()
    
        End Sub
    
    
        Sub RefreshGrid()
            Sql = clsSetup.LocationGrid_SQL("ALL")
            dt = clsSetup.populateGrid(Sql)
            grid1.DataSource = dt
            grid1.DataBind()
            Sql = clsSetup.LocationGrid_SQL("Pending")
            dt = clsSetup.populateGrid(Sql)
            grid2.DataSource = dt
            grid2.DataBind()
    
         
        End Sub
    
    
    End Class



    Error
    I am getting error on pointer - grid1.DataSource = dt

    error message

    System.NullReferenceException was unhandled by user code
    Message=Object reference not set to an instance of an object.
    Source=EPayments
    StackTrace:
    at EPayments.frmLocations.RefreshGrid() in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\EPayments\EPayments\Setups\frmLocati ons.aspx.vb:line 28
    at EPayments.frmLocations.Page_Load(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\EPayments\EPayments\Setups\frmLocati ons.aspx.vb:line 20
    at System.Web.UI.Control.OnLoad(EventArgs e)
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException:



    Pls assist
    Thanks

  2. #2
    skaswani is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    16
    Reputation
    59
    pls help..

  3. #3
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    Set a breakpoint in the first line of the Page_Load method and run the code (to set a breakpoint in Visual Studio, click in the margin beside the line you want to break at and a red dot should appear). When the code stops at that line, step past the line then hover your mouse over the grid1 variable. A pop-up will let you know the value of the variable.

    If the problem isn't immediately noticeable, set other breakpoints and verify the value of other variables.

    Let us know if you have other questions.
    — Stephen Paszt

  4. #4
    skaswani is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    16
    Reputation
    59
    error is showing on pointer:

    grid1.DataSource = dt

    dt has 2 record count, whereas grid1 is nothing

  5. #5
    zoller is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    May 2012
    Location
    New Zealand
    Posts
    9
    Reputation
    0
    The problem is
    cltGrid.FindControl("GridView1") returns nothing.

    I think you should check the code in your user control and confirm that a control with that ID actually exists, and if you declared it programmatically, ensure you aren't missing 'new' on instantiation
    Zeb Rawnsley

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking