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

skaswani

Member
Joined
Oct 5, 2008
Messages
20
Programming Experience
Beginner
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


VB.NET:
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\frmLocations.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\frmLocations.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
 
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.
 
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
 
Back
Top