title: Holding dynamic Controls on postback not working
body: <p>I get this error and do not find the problem:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>My Code:</p>
  
  <p>Page Init:</p>
  
  <pre><code>else if (Request.QueryString["CompanyID"] != null)
  {
     pnlComp.Visible = true;
     if (Convert.ToString(ViewState["Generated"]) != "true")
     {
        createCompSite();
        ViewState["Generated"] = "true";
     }                
  }
  </code></pre>
  
  <p>Page Load:</p>
  
  <pre><code> Verification.Check();
   if (!IsPostBack)
   {
      sFilter= null;
      FillGridviewAndValues();
   }
   if (Convert.ToString(ViewState["Generated"]) == "true")
      createCompSite();
  </code></pre>
  
  <p>Function:</p>
  
  <pre><code>TableHeaderRow tHRow = new TableHeaderRow();
  TableHeaderCell tHeader = new TableHeaderCell();
  tHeader.Text = dr.Field&lt;string&gt;("Loc");
  tHRow.Cells.Add(tHeader);
  tblCompLoc1.Rows.Add(tHRow);
  
  DataTable dtCat = new DataTable();
  dtCat = SQLCon.SQLCommand_DokuTool("SELECT CL.ID, C.CatName FROM TBL_Ref_CatLoc AS CL, TBL_Category AS C WHERE C.CatID = CL.CatID AND CL.LocID = '" + dr.Field&lt;Guid&gt;("ID").ToString() + "' AND CL.Approved='True' ORDER BY CatName ASC");
  
  foreach (DataRow dr2 in dtCat.Rows)
  {
     //Working as plain Text
     //TableRow tRow = new TableRow();
     //TableCell tCell = new TableCell();
     //tCell.Text = dr2.Field&lt;string&gt;("CatName");
     //tRow.Cells.Add(tCell);
     //tblCompLoc1.Rows.Add(tRow);
  
  
     //Not working as Hyperlink or Btn
       TableRow tRow = new TableRow();
       TableCell tCell = new TableCell();
       HyperLink hl = new HyperLink();
       hl.EnableViewState=true;
       hl.Text = dr2.Field&lt;string&gt;("CatName");
       string gLocID = SQLCon.SQLCommand_User("SELECT ID FROM TBL_Location WHERE fCompanyID = '"+sRequest+"' AND City + ', ' + Street1 ='"+tHeader.Text+"'").Rows[0].Field&lt;Guid&gt;("ID").ToString();
       string gCatID = SQLCon.SQLCommand_DokuTool("SELECT CatID FROM TBL_Category WHERE CatName='"+hl.Text+"'").Rows[0].Field&lt;Guid&gt;("CatID").ToString();
       string sRequestedCat = SQLCon.SQLCommand_DokuTool("SELECT ID FROM TBL_Ref_CatLoc WHERE LocID = '"+gLocID+"' AND CatID='"+gCatID+"'").Rows[0].Field&lt;Guid&gt;("ID").ToString();                        
       hl.NavigateUrl="~/Default?ID="+sRequestedCat;
       tCell.Controls.Add(hl);
       tRow.Controls.Add(tCell);
       tblCompLoc1.Rows.Add(tRow);
  }
  </code></pre>
  
  <p>I want to create a Table which is filled from DataBase and can look different on each site and every Table Cell should have a different Link where it leads the user. The table creation starts on page init.</p>