Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have creating controls dynamically successfully but when i applied master page then throwing an error "Object reference not set to an instance of an object." below is the my code.

protected void GenerateTableOnDelete(int rowsCount, int deleteID)
        {
            rowsCount--;
            Table table = new Table();
            table.ID = "Table1";
            PlaceHolder1.Controls.Add(table);
            const int colsCount = 6;
            TableHeaderRow header = new TableHeaderRow();
            TableHeaderCell headerTableCell1 = new TableHeaderCell();
            headerTableCell1.Text = "Item Name";
            header.Cells.Add(headerTableCell1);                
            table.Rows.Add(header);
            for (int i = 0, k = 0; i < rowsCount; i++)
            {
                if (i < deleteID)
                    k = i;
                else
                    k = i - 1;
                if (deleteID != i)
                {
                    TableRow row = new TableRow();
                    row.ID = "Row_" + k;
                    for (int j = 0; j < colsCount; j++)
                    {
                        if (j == colsCount - 6)
                        {
                            TableCell cell = new TableCell();
                            DropDownList ddl = new DropDownList();                           
                            ds = fetchStates();
                            ddl.DataSource = ds.Tables[0];
                            ddl.DataValueField = "EmpId";
                            ddl.DataTextField = "EmpName";
                            ddl.DataBind();
                            ddl.ID = "DropDownListRow_" + k + "Col_" + j;
                            cell.Controls.Add(ddl);
                            row.Cells.Add(cell);
                        }

                    }
                    table.Rows.Add(row);
                }
            }
            SetPreviousDataOnDelete(rowsCount, colsCount, deleteID);
            ViewState["RowsCount"] = rowsCount;
        }


at this line getting error Line 49: Table table = (Table)this.Page.FindControl("PlaceHolder1").FindControl("Table1"); // **** if (table != null) {

private void SetPreviousData(int rowsCount, int colsCount)
        {
            Table table = (Table)this.Page.FindControl("ContentPlaceHolder1").FindControl("Table1"); // ****         if (table != null) {
            for (int i = 0; i < rowsCount; i++)
            {
                for (int j = 0; j < colsCount; j++)
                {
                    if (j == colsCount - 6)
                    {
                        //Extracting the Dynamic Controls from the Table
                        DropDownList ddl = (DropDownList)table.Rows[i+1].Cells[j].FindControl("DropDownListRow_" + i + "Col_" + j);
                        //Use Request object for getting the previous data of the dynamic textbox
                        ddl.Text = Request.Form["DropDownListRow_" + i + "Col_" + j];//*****                 }                
                    }
}
            }
        }
Posted
Comments
Andy Lanng 1-Jun-15 4:51am    
check if ContentPlaceHolder1 exists before you perform the second findcontrol.

Didn't you call it PlaceHolder1?

Be careful with names when you use findcontrol. There is no compile time checking (but you knew that already)
Asma Siddiqua 1-Jun-15 4:58am    
i changed like this.
//Generate Table
ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
Table table = new Table();
table.ID = "Table1";
content.Controls.Add(table);
//setting previous data
Table table = (Table)this.Page.FindControl("ContentPlaceHolder1").FindControl("Table1");

but again getting same error.
F-ES Sitecore 1-Jun-15 5:34am    
Check the order your code runs in, check the controls are added before you find them using FindControl

1.The problem is generated by the first FindControl("ContentPlaceHolder1") call, that cannot find your control, maybe because you are using a wrong control ID.

2.Your placeholder is a server object and you should access it directly like you did in your GenerateTableOnDelete method. So you should change your code with problem like this:
C#
Table table = (Table)PlaceHolder1.Controls.FindConntrol("Table1");
if(table != null)
{
 // Your code.
 //...
}
 
Share this answer
 
Comments
Asma Siddiqua 1-Jun-15 5:09am    
I am using correct id as it is which is in .aspx page.
and what is 'placeholder1'? where iam adding table to 'ContentPlaceHolder1'
Kornfeld Eliyahu Peter 1-Jun-15 5:10am    
The problem IS with ContentPlaceHolder1, but not an ID problem...
ContentPlaceHolder1 is the placeholder declared on the master page and for that is not part of the content page!!!
The solution should be simple Page.FindControl("Table1")...
OP must realize that adding master to a page should not change the code of the page itself...The object model of a page does not depend on its master(s)...
Asma Siddiqua 1-Jun-15 5:17am    
when iam accessing table like this then same error throwing at "DropDownList ddl = (DropDownList)table.Rows[i+1].Cells[j].FindControl("DropDownListRow_" + i + "Col_" + j);"
so how to add the table in contentplaceholder?
got the answer..have to access like below.
 private void SetPreviousData(int rowsCount, int colsCount)
        {
            ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
            PlaceHolder plz = (PlaceHolder)content.FindControl("PlaceHolder1");
            Table table = (Table)plz.FindControl("Table1");
/*--------------
-------------*/
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900