title: ASP.Net C# - Concatenating a variable to access controls
body: <p>My original function is below, however, I'd like to modify this so that I can pass in a number to update the necessary controls e.g. getQuestionDetails(3) would update lblQuestion3.Text and lblQuestion3Type.Text.</p>
  
  <p>However, my attempt at this only seems to work when I execute getQuestionDetails(1), getQuestionDetails(2), getQuestionDetails(3) in that order.  If I try to execute getQuestionDetails(3) first, nothing happens.  Any ideas why?</p>
  
  <p>original code:</p>
  
  <pre><code>protected void getQuestionDetails()
  {
      string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
      MySqlConnection conn = new MySqlConnection(connStr);
      MySqlDataReader reader;
  
      string cmdText = "SELECT * FROM questions WHERE question_id=@QuestionID";
      MySqlCommand cmd = new MySqlCommand(cmdText, conn);
      cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
      cmd.Parameters["@QuestionID"].Value = Convert.ToInt32(ddlQuestion1IDs.SelectedValue);
  
      try
      {
          conn.Open();
          reader = cmd.ExecuteReader();
          if (reader.Read())
          {
              lblQuestion1.Text = reader["question"].ToString();
  
              if (reader["type"].ToString().Equals("T"))
              {
                  lblQuestion1Type.Text = "simple question - one word answer";
              }
              else if (reader["type"].ToString().Equals("R"))
              {
                  lblQuestion1Type.Text = "multiple choice - one correct answer";
              }
              else if (reader["type"].ToString().Equals("C"))
              {
                  lblQuestion1Type.Text = "multiple choice - more than one correct answer";
              }
          }
          reader.Close();
      }
      catch
      {
          lblError.Text = "Database connection error - failed to get question details.";
      }
      finally
      {
          conn.Close();
      }
  }
  </code></pre>
  
  <p>my attempt:</p>
  
  <pre><code>protected void getQuestionDetails(string a)
  {
      string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
      MySqlConnection conn = new MySqlConnection(connStr);
      MySqlDataReader reader;
  
      string cmdText = "SELECT * FROM questions WHERE question_id=@QuestionID";
      MySqlCommand cmd = new MySqlCommand(cmdText, conn);
      cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
      cmd.Parameters["@QuestionID"].Value = Convert.ToInt32(((DropDownList)this.FindControl("ddlQuestion" + a + "IDs")).SelectedValue);
  
      try
      {
          conn.Open();
          reader = cmd.ExecuteReader();
          if (reader.Read())
          {
              ((Label)this.FindControl("lblQuestion" + a)).Text = reader["question"].ToString();
  
              if (reader["type"].ToString().Equals("T"))
              {
                  ((Label)this.FindControl("lblQuestion" + a + "Type")).Text = "simple question - one word answer";
              }
              else if (reader["type"].ToString().Equals("R"))
              {
                  ((Label)this.FindControl("lblQuestion" + a + "Type")).Text = "multiple choice - one correct answer";
              }
              else if (reader["type"].ToString().Equals("C"))
              {
                  ((Label)this.FindControl("lblQuestion" + a + "Type")).Text = "multiple choice - more than one correct answer";
              }
          }
          reader.Close();
      }
      catch
      {
          lblError.Text = "Database connection error - failed to get question details.";
      }
      finally
      {
          conn.Close();
      }
  }
  </code></pre>