473,394 Members | 1,769 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,394 software developers and data experts.

adding controls at runtime

hi,
i am trying to add a datagrid control to a web page dynamically.
i am able to do it while i used the following code in the page load
event of the form in which i am going to add the control.

DataGrid dG = new DataGrid();
Control frm = FindControl("frmSnAInformation");
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
since i need to do the same process for many forms i have written the
code in a class file and call the function when needed. also i am
passing the form name as parameter. the class file function is as
follows:

public void funGrid(string frmNam)
{
DataGrid dG = new DataGrid();
Control frm = FindControl(frmANam);
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}
but when i try to add control i am getting the following error:

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

at the line: frm.Controls.Add(dG);
what could be the reason and the work around for this problem.

regards,
kalaivanan

Feb 7 '07 #1
2 1687
Hi there,

This line is likely the problem:
Control frm = FindControl(frmANam);

The problem is that you are trying to get a reference to the htmlform with
name passed by frmName parameter, but such form does not exist on the page.
try to review your code and see if you're passing correct ID of the control.
--
Milosz
"kalaivanan" wrote:
hi,
i am trying to add a datagrid control to a web page dynamically.
i am able to do it while i used the following code in the page load
event of the form in which i am going to add the control.

DataGrid dG = new DataGrid();
Control frm = FindControl("frmSnAInformation");
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
since i need to do the same process for many forms i have written the
code in a class file and call the function when needed. also i am
passing the form name as parameter. the class file function is as
follows:

public void funGrid(string frmNam)
{
DataGrid dG = new DataGrid();
Control frm = FindControl(frmANam);
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}
but when i try to add control i am getting the following error:

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

at the line: frm.Controls.Add(dG);
what could be the reason and the work around for this problem.

regards,
kalaivanan

Feb 7 '07 #2
On Feb 7, 1:36 am, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Hi there,

This line is likely the problem:
Control frm = FindControl(frmANam);

The problem is that you are trying to get a reference to the htmlform with
name passed by frmName parameter, but such form does not exist on the page.
try to review your code and see if you're passing correct ID of the control.
--
Milosz

"kalaivanan" wrote:
hi,
i am trying to add a datagrid control to a web page dynamically.
i am able to do it while i used the following code in the page load
event of the form in which i am going to add the control.
DataGrid dG = new DataGrid();
Control frm = FindControl("frmSnAInformation");
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
since i need to do the same process for many forms i have written the
code in a class file and call the function when needed. also i am
passing the form name as parameter. the class file function is as
follows:
public void funGrid(string frmNam)
{
DataGrid dG = new DataGrid();
Control frm = FindControl(frmANam);
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}
but when i try to add control i am getting the following error:
System.NullReferenceException: Object reference not set to an instance
of an object.
at the line: frm.Controls.Add(dG);
what could be the reason and the work around for this problem.
regards,
kalaivanan- Hide quoted text -

- Show quoted text -
you are absolutely right man.
i changed my code as given below;
since the code is executed in the class file the frm is assigned null.

Control frm = FindControl(frmANam);

hence i added one more parameter to the function.

public void funGrid(string frmANam, System.Web.UI.Page pg)
{
DataGrid dG = new DataGrid();
Control frm = pg.FindControl(frmANam);
frm.Controls.Add(dG);

DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_al l");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}

I passed 'this' keyword as the second argument from the form in which
i call this funtion.
now 'frm' is not null and working too.

regards,
kalaivanan

Feb 7 '07 #3

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

Similar topics

2
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
8
by: Thomas H. | last post by:
Hi folks, what's the best way to add (web)controls at runtime ?? TIA T.H.
4
by: slylos | last post by:
I've got a Windows form that has a TabControl. I add TabPages to this TabControl at Runtime, along with two Textboxes and a button. The problem I'm having is I can't figure out how to reference...
2
by: Mark Siffer | last post by:
I am trying to add a compare validator at runtime. I would add it before but my input controls are built at runtime via an editable datagrid. Therefore, the names of the controls are not known...
0
by: Kurt | last post by:
Hi Brent, I don't know if this is best practices per se`, but it works. The following generates a new button control each time menu item 1 is clicked and assigns an event handler to it. ...
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
3
by: Toe Dipper | last post by:
In short we have a lengthy process when a form is loaded that adds activex controls to our windows form. This process in itself works fine however we would like to push this processing to a thread...
0
by: sonic | last post by:
I am trying to dynamically load a validator and must be missing something elementary here. I extended TextBox control to add some functionality to it. One new feature it contains is IsRequired...
11
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.