Problem in adding controls dynamically
I am trying to create multiple datagrid ,
Below is the server error that i got while executing.
Server Error in '/threaad' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 38: for(int i=1;i<=5;i++)
Line 39: {
Line 40: dg[i].DataSource = ds;
Line 41: dg[i].DataBind();
Line 42: place1.Controls.Add(dg[i]);
Source File: c:\inetpub\wwwroot\threaad\dynmicdatagrid.aspx.cs Line: 40
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
threaad.DynmicDatagrid.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\threaad\dynmicdatagrid.aspx.cs: 40
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
&&& the code -snippet that i executed is below.
SqlConnection con;SqlDataAdapter myda;
protected System.Web.UI.WebControls.PlaceHolder place1;
DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataGrid [] dg = new DataGrid[5];
//DataGrid dg = new DataGrid();
ds = new DataSet();
con = new SqlConnection("server=deepsea;database=master;user id=sa;password=****");
string qry = "SELECT TOP 100 * FROM PRODUCTS ";
myda = new SqlDataAdapter(qry,con);
myda.Fill(ds);
// dg.DataSource = ds;
// dg.DataBind();
// place1.Controls.Add(dg);
for(int i=1;i<=5;i++)
{
dg[i].DataSource = ds;
dg[i].DataBind();
place1.Controls.Add(dg[i]);
}
}
Vivek Shah
__________________
Vivek Shah
|