473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

UserControl with Gridview: Set DataSource: NullReferenceException

OJ
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

Apr 28 '06 #1
7 7361
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

Apr 28 '06 #2
OJ
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

Apr 28 '06 #3
OJ
forgot to add that the above code is in a button click event...

Thanks,
O

Apr 28 '06 #4
OJ
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

Apr 28 '06 #5
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.

Apr 28 '06 #6
OJ
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

Apr 28 '06 #7
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.

Apr 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: j.zascinski | last post by:
Hi, i have a "simple" problem with gridview, please help me :) i want to have gridview which is binded to a datatable (or a dataset). i can show the data from the dataset in the gridview and i can...
1
by: K B | last post by:
Hi. I have a gridview with sqldatasource, etc. For the grid, I also have a search textbox. If the user needs to filter the records, I want to dynamically adjust the SELECT statement for the...
5
by: amatuer | last post by:
hi i have a link from page1 to page2. this link uses a querystring to transfer data. usually i have no problems using querystring t transfer data. bt for the first time page2 does not display.an...
0
by: Tarren | last post by:
Hi: I have a formview and I want a gridview to show at the bottom of the page beneath the formview. The thing is that the datasource for the gridview had ControlParameters which need to pull...
0
by: VMI | last post by:
My web app, during Page_Load, fills up my gridview with a datatable. I have a button under the gridview that adds rows to the gridview. Why is it that gridview.datasource is null inside the...
0
by: shapper | last post by:
Hello, I defined my GridView datasource on its Init event. Then I used DataBind. When a button is clicked I need to Bind again my GridView because some rows on my DataSource were changed. Do...
7
by: =?Utf-8?B?U0FM?= | last post by:
I am trying to cast my Gridview in my Code behide as a Dataset so I can manipulate the data. Here is the code snippet: Dim FilteredDataSet As New System.Data.DataSet Dim gv As New GridView gv...
1
by: speranza | last post by:
Hi, İ dont know how to assign sql command query value to a gridview..i am trying like this..it dosnt work can you help me? Protected Sub Button1_Click(ByVal sender As Object, ByVal e As...
1
fayazmd
by: fayazmd | last post by:
Hi, I have worked in various projects. In some projects GridView binding is done by DataTable and in other by IList(List<ClassName>). Most client projects they have followed this IList<> and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.