title: Initializing Table Adapter Rows
body: <p>Is it possible to initialize table adapter rows? I have the following code I've created; refactored from duplication of control names to recursively finding controls with FindControl:</p>
  
  <pre><code>private int _category;
  private int _max;
  private string _queryString;
  private int _pageStart;
  private const int PageSize = 4;
  private void PopulatePage(string s)
  
  {
      using (var projectTableAdapter = new ProjectTableAdapter())
      {
          _max = s == "category" ? projectTableAdapter.GetDataByCategory(_category).Count : PageSize;
          double totalPages = Math.Ceiling((float) _max/PageSize);
  
          int start = (_pageStart - 1) * PageSize;
          var end = _pageStart * PageSize;
  
          if (end &gt; _max) end = _max;
  
          for (int i = start; i &lt; end; i++)
          {
              int tmp = i - start;
              int c = tmp + 1;
  
              var image = FindControl(string.Format("ctl00$Body$ControlImage{0}", c)) as Image;
              var title = FindControl(string.Format("ctl00$Body$ControlTitle{0}", c)) as Literal;
              var summary = FindControl(string.Format("ctl00$Body$ControlSummary{0}", c)) as Literal;
              var link = FindControl(string.Format("ctl00$Body$ControlLink{0}", c)) as HyperLink;
              var listitem = FindControl(string.Format("ctl00$Body$ControlListItem{0}", c)) as HtmlGenericControl;
  
              switch (s)
              {
                  case "category":
                      DataConnection.ProjectRow projectRowCategory =
                          projectTableAdapter.GetDataByCategory_Active_RankOrder(_category)[i];
  
                      if (image != null) image.ImageUrl = projectRowCategory.Image;
                      if (title != null) title.Text = projectRowCategory.Name;
                      if (summary != null) summary.Text = projectRowCategory.Summary;
  
                      if (projectRowCategory.Description.Length &gt; 0)
                      {
                          if (link != null)
                          {
                              link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                               projectRowCategory.ProjectID);
                              link.Visible = true;
                          }
                      }
                      break;
                  case "random":
                          DataConnection.ProjectRow projectRowRandom = projectTableAdapter.GetDataByRandom()[i];
  
                          if (image != null) image.ImageUrl = projectRowRandom.Image;
                          if (title != null) title.Text = projectRowRandom.Name;
                          if (summary != null) summary.Text = projectRowRandom.Summary;
  
                          if (projectRowRandom.Description.Length &gt; 0)
                          {
                              if (link != null)
                              {
                                  link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                                   projectRowRandom.ProjectID);
                                  link.Visible = true;
                              }
                          }
                      break;
              }
  
              if (listitem != null) listitem.Visible = true;
          }
  
          if ((Request.QueryString["p"] == null) || (Request.QueryString["p"] == "1"))
          {
              if (_max &lt; PageSize)
              {
                  ControlPage.Text = "";
              }
              else
              {
                  ControlPage.Text = "&lt;&lt; prev | &lt;a href = \"?s=" + _queryString + "&amp;p=2\"&gt;next &gt;&gt;&lt;/a&gt;";
              }
          }
          else
          {
              int next = _pageStart + 1;
              int prev = _pageStart - 1;
              if (next &gt; totalPages)
              {
                  ControlPage.Text = string.Format(@"&lt;a href = ""?s={0}&amp;p={1}""&gt;&lt;&lt; prev&lt;/a&gt; | next &gt;&gt;&lt;/a&gt;",
                                                   _queryString, prev);
              }
              else
              {
                  ControlPage.Text =
                      string.Format(
                          @"&lt;a href = ""?s={0}&amp;p={1}""&gt;&lt;&lt; prev&lt;/a&gt; | &lt;a href = ""?s={0}&amp;p={2}""&gt;next &gt;&gt;&lt;/a&gt;",
                          _queryString, prev, next);
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to now refactor and remove code duplication within the SWITCH statement. So I can do something like:</p>
  
  <pre><code>var projectRow = new DataConnection.ProjectRow();
  
              switch (s)
              {
                  case "category":
                          projectRow = projectTableAdapter.GetDataByCategory_Active_RankOrder(_category)[i];
                      break;
                  case "random":
                          projectRow = projectTableAdapter.GetDataByRandom()[i];
                      break;
              }
  
              if (image != null) image.ImageUrl = projectRow.Image;
              if (title != null) title.Text = projectRow.Name;
              if (summary != null) summary.Text = projectRow.Summary;
  
              if (projectRow.Description.Length &gt; 0)
              {
                  if (link != null)
                  {
                      link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                       projectRow.ProjectID);
                      link.Visible = true;
                  }
              }
  
              if (listitem != null) listitem.Visible = true;
  </code></pre>
  
  <p>Since ProjectRow is an internal constructor of DataConnection that code is a no go. It should be doable I think...my brain just went on vacation after Cinco de Mayo it seems.</p>
  
  <p>UPDATE:</p>
  
  <p>Got it working using the following method. Forgot to just set the row initializer to 'null' and not using 'new'. Thanks for the information too, Henk.</p>
  
  <pre><code>DataConnection.ProjectRow projectRow = null;
  
              switch (s)
              {
                  case "category":
                      projectRow = projectTableAdapter.GetDataByCategory_Active_RankOrder(_category)[i];
                      break;
                  case "random":
                      projectRow = projectTableAdapter.GetDataByRandom()[i];
                      break;
              }
  
              if (image != null) if (projectRow != null) image.ImageUrl = projectRow.Image;
              if (title != null) if (projectRow != null) title.Text = projectRow.Name;
              if (summary != null) if (projectRow != null) summary.Text = projectRow.Summary;
  
              if (projectRow != null &amp;&amp; projectRow.Description.Length &gt; 0)
              {
                  if (link != null)
                  {
                      link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                       projectRow.ProjectID);
                      link.Visible = true;
                  }
              }
  
              if (listitem != null) listitem.Visible = true;
  </code></pre>