PC Review


Reply
Thread Tools Rate Thread

adding controls at runtime

 
 
kalaivanan
Guest
Posts: n/a
 
      7th Feb 2007
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_all");
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_all");
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      7th Feb 2007
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_all");
> 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_all");
> 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
>
>

 
Reply With Quote
 
kalaivanan
Guest
Posts: n/a
 
      7th Feb 2007
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_all");
> > 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_all");
> > 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_all");
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding controls at Runtime =?Utf-8?B?c2x5bG9z?= Microsoft C# .NET 4 17th Jul 2005 04:04 AM
Adding Controls at runtime ? Thomas H. Microsoft C# .NET 8 5th Jan 2005 02:06 PM
Help with adding controls at runtime X-Men Microsoft Access Forms 1 10th Feb 2004 12:48 AM
Re: Adding Controls at Runtime Herfried K. Wagner [MVP] Microsoft VB .NET 0 1st Oct 2003 09:46 PM
Re: Adding Controls at Runtime Tom Shelton Microsoft VB .NET 0 1st Oct 2003 07:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 AM.