UserControl with Gridview: Set DataSource: NullReferenceException

Discussion in 'ASP .Net' started by OJ, Apr 28, 2006.

  1. OJ

    OJ Guest

    Hi,
    I have a WebUserCOntrol with a GridView and I am attempting to set the
    DataSource of the Gridview in the User control from the parent ASPX
    page. I have the following property in the user control...

    public DataTable DataSourceTable
    {
    get
    {
    return this.gvwMain.DataSource as DataTable;
    }
    set
    {
    this.gvwMain.DataSource = value;
    }
    }


    Where gvwMain is the name of the gridview. However, when I try to set
    the datasource I get a NullReferenceException saying that gvwMain (the
    gridview) is Null....what am I missing then? In 2.0 using VWD2005...

    Thanks,
    Oli
     
    OJ, Apr 28, 2006
    #1
    1. Advertising

  2. In what part of the page event process are you accessing this property?

    Shaun McDonnell

    > Hi,
    > I have a WebUserCOntrol with a GridView and I am attempting to set the
    > DataSource of the Gridview in the User control from the parent ASPX
    > page. I have the following property in the user control...
    > public DataTable DataSourceTable
    > {
    > get
    > {
    > return this.gvwMain.DataSource as DataTable;
    > }
    > set
    > {
    > this.gvwMain.DataSource = value;
    > }
    > }
    > Where gvwMain is the name of the gridview. However, when I try to set
    > the datasource I get a NullReferenceException saying that gvwMain (the
    > gridview) is Null....what am I missing then? In 2.0 using VWD2005...
    >
    > Thanks,
    > Oli
     
    Shaun McDonnell, Apr 28, 2006
    #2
    1. Advertising

  3. OJ

    OJ Guest

    Hi Shaun,
    Thanks..I create a new instance of the UserCOntrol like this..

    UserControls_SABSHolidays holidayTable = new
    UserControls_SABSHolidays();

    Try to set the datasource....(gvwMainDS is a datatable with 100 rows)

    holidayTable.DataSourceTable = gvwMainDS;

    which calls the code in my first post....

    Then add it to the pages content holder...
    Master.MiddleContent.Controls.Add(holidayTable);

    O
     
    OJ, Apr 28, 2006
    #3
  4. OJ

    OJ Guest

    forgot to add that the above code is in a button click event...

    Thanks,
    O
     
    OJ, Apr 28, 2006
    #4
  5. OJ

    OJ Guest

    Hi,
    when the ContentPlaceHolder already has the UserControl this code
    works. It is only when I try to add it programmatically....

    I guess that means it has something to do with the Page lifecycle, but
    I dont really know where to start looking. When to the server controls
    in an ASCX Usercontrol become initialized. Can I overrride the New
    method perhaps? Anyone?

    Thanks
    O
     
    OJ, Apr 28, 2006
    #5
  6. OJ

    Thiru.net Guest

    hi oli,
    i guess you tried to create a user control as you do in windows
    application.

    webuser controls are different from windows user controls.

    though you assign datatable value to the datagrid datasource, when you
    access the datatable assigned, it will say NullReferenceException only.
    because when you submit your parent again, the webcontrol loses the
    datagrid's datasource value.

    so you have the store in Datatable which you assign in ViewState for
    the value to be persist ed throughout all the page submits.
     
    Thiru.net, Apr 28, 2006
    #6
  7. OJ

    OJ Guest

    Hi,
    The data is new. The user control caches the table before it trys to
    set the datasource of the gridview. The data is all there and present.
    It seems that the GridView in the UserCOntrol has not been initialized
    when I call a new instance of the UserControl. The usercontrol exists
    as does the reference to the gridview but it seems the gridview itself
    has not yet been initialised. Does anybody know when server controls
    inside a WebUserControl are actually instantiated?

    Thanks,
    Oli
     
    OJ, Apr 28, 2006
    #7
  8. Exactly right. Nice.

    Shaun McDonnell

    > hi oli,
    > i guess you tried to create a user control as you do in windows
    > application.
    > webuser controls are different from windows user controls.
    >
    > though you assign datatable value to the datagrid datasource, when you
    > access the datatable assigned, it will say NullReferenceException
    > only. because when you submit your parent again, the webcontrol loses
    > the datagrid's datasource value.
    >
    > so you have the store in Datatable which you assign in ViewState for
    > the value to be persist ed throughout all the page submits.
    >
     
    Shaun McDonnell, Apr 28, 2006
    #8
    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. Angel Faus
    Replies:
    2
    Views:
    1,045
    Jeff Washburn
    Dec 1, 2003
  2. Rob Van Zant
    Replies:
    0
    Views:
    518
    Rob Van Zant
    Nov 10, 2003
  3. PatricQ
    Replies:
    3
    Views:
    9,620
    PatricQ
    Feb 18, 2004
  4. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.NullReferenceException: Object reference not set to an inst

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,593
    Ken Dopierala Jr.
    Oct 6, 2004
  5. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.NullReferenceException: Object reference not set to an inst

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Nov 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    549
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Nov 5, 2004
Loading...

Share This Page