title: c# Dynamically create a table on webform based on query and add textboxes instead of values
body: <p>I am very new to C#. I have decided to develop a system, using webforms in C#, to record punter entries to a sweep for an annual competition. This is hopefully the best way to learn C# properly.</p>
  
  <p>I can dynamically create the table on the webform based on a straightforward query which returns the home and away teams, with scores and other ancillary data. However, in order to use the same, or similar query, I need to create the same table which will insert textboxes, to replace the scores, which will be filled with the punters' predictions for scores for each game. There is a finite number of games.</p>
  
  <p>Any and all helpful suggestions are welcome. Sarcasm and derogatory remarks are not appreciated as I assume you all can appreciate. If required, I can PM the SQL query to quickly build the database if required. </p>
  
  <p>Here is my code so far:</p>
  
  <p><strong>entry_form.aspx</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="entry_form.aspx.cs" Inherits="Six_Nations_Championship.ShowData" %&gt;
  
  
  
    &lt;!DOCTYPE html&gt;
    &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  
    &lt;head id="Head2" runat="server"&gt;
      &lt;title&gt;Six Nations Sweep - Entry Form&lt;/title&gt;
      &lt;link href="StyleSheet.css" rel="stylesheet" /&gt;
    &lt;/head&gt;
  
    &lt;body&gt;
      &lt;form id="form2" runat="server"&gt;
        &lt;div id="main_div"&gt;
          &lt;div id="banner"&gt;
          &lt;/div&gt;
          &lt;div id="entry_form" class="form-style-9"&gt;
            &lt;div id="Div4"&gt;
              &lt;table&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;b class="field-style field-sptdt atdgn-left"&gt;First Name&lt;/b&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;asp:TextBox ID="FName" CssClass="textbox" runat="server" BackColor="White"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;b class="field-style field-sptdt atdgn-left"&gt;Last Name&lt;/b&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;asp:TextBox ID="LName" CssClass="textbox" runat="server" BackColor="White"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;b class="field-style field-sptdt atdgn-left"&gt;Mobile Number&lt;/b&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;asp:TextBox ID="Mobile" CssClass="textbox" runat="server" BackColor="White"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;b class="field-style field-sptdt atdgn-left"&gt;Email Address&lt;/b&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;asp:TextBox ID="Email" CssClass="textbox" runat="server" BackColor="White"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;
            &lt;/div&gt;
            &lt;table class="results"&gt;
              &lt;tr&gt;
                &lt;td&gt;
                  &lt;asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  
    &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>entry_form.aspx.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Text;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.ComponentModel;
  using System.Drawing;
  
  namespace Six_Nations_Championship
  {
              public partial class ShowData : System.Web.UI.Page
          {
              SqlDataAdapter da;
              DataSet ds = new DataSet();
              StringBuilder htmlTable = new StringBuilder();
  
              protected void Page_Load(object sender, EventArgs e)
              {
                  if (!Page.IsPostBack)
                      BindData();
              }
  
              private void BindData()
              {
                  SqlConnection con = new SqlConnection();
                  con.ConnectionString = @"Data Source=SEAN_LAPTOP-HP;Initial Catalog=Six_Nations;Integrated Security=True";
                  SqlCommand cmd = new SqlCommand("SELECT match_schedule.gameid AS Game, Home_Teams.country_name AS Home, match_schedule.home_country_score AS HomeScore,  match_schedule.away_country_score AS AwayScore, Away_Teams.country_name AS Away, match_schedule.venue, match_schedule.gametime AS Date FROM Away_Teams INNER JOIN match_schedule ON Away_Teams.away_teamid = match_schedule.away_teamid INNER JOIN Home_Teams ON match_schedule.home_teamid = Home_Teams.home_teamid", con);
                  da = new SqlDataAdapter(cmd);
                  da.Fill(ds);
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();
  
                  htmlTable.Append("&lt;table class='results'&gt;");
                  htmlTable.Append("&lt;tr&gt;&lt;th&gt;Game ID&lt;/th&gt;&lt;th&gt;First Name&lt;/th&gt;&lt;th&gt;Last Name&lt;/th&gt;&lt;th&gt;Home&lt;/th&gt;&lt;th&gt;Score&lt;/th&gt;&lt;th&gt;Score&lt;/th&gt;&lt;th&gt;Away&lt;/th&gt;&lt;/tr&gt;");
  
                  if (!object.Equals(ds.Tables[0], null))
                  {
                      if (ds.Tables[0].Rows.Count &gt; 0)
                      {
  
                          for (int i = 0; i &lt; ds.Tables[0].Rows.Count; i++)
                          {
                              htmlTable.Append("&lt;tr style='color: White;'&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["Game"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["Home"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["HomeScore"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["AwayScore"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["Away"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["Venue"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;td&gt;" + ds.Tables[0].Rows[i]["Date"] + "&lt;/td&gt;");
                              htmlTable.Append("&lt;/tr&gt;");
                          }
                          htmlTable.Append("&lt;/table&gt;");
                          DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
                      }
                      else
                      {
                          htmlTable.Append("&lt;tr&gt;");
                          htmlTable.Append("&lt;td align='center' colspan='4'&gt;There is no Record.&lt;/td&gt;");
                          htmlTable.Append("&lt;/tr&gt;");
                      }
                  }
              }
              }
  }
  </code></pre>