title: Cannot get SqlDataSource.SelectParameters.Add to work
body: <p>I am trying to change the parameter of an SqlDataSource on the fly, but the code seems to fail, or in other words I seem to fail.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>        int id = 0;
  
          SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RHOGEConnection"].ConnectionString);
          SqlCommand getId = new SqlCommand(null, conn);
          SqlParameter name = new SqlParameter("@name", SqlDbType.NVarChar);
          name.Value = SelectTeam.SelectedValue;
          getId.Parameters.Add(name);
          /* getId.CommandText = */ //changed to a select statement that gets a number
  
          try
          {
              conn.Open();
              id = (int)getId.ExecuteScalar();
              conn.Close();
          }
          catch (Exception ex)
          {
              Response.Write(ex.Message);
          }
  
          SqlDataSource2.SelectParameters.Add(new Parameter("getId", TypeCode.Int16, Convert.ToString(id)));
  </code></pre>
  
  <p>Now I know that the code to get the number, the lines above SqlDataSource2, works because I printed it on the screen and they appear correct. I also have tried the SqlDataSource2 select command in SQL Management Studio and it produces the correct results. So my best guess is there is a problem in adding the parameter to the parameters list.</p>
  
  <pre><code>SELECT [TeamName] FROM [Teams] where PlayerIdent = (select playerId
                  from Players where PlayerShirtId = @getId)
  </code></pre>
  
  <p>The above is the select command that works.</p>
  
  <p>Do you agree that the parameter thing is the correct diagnosis, and if so what I should do?</p>
  
  <p>EDIT: I was able to solve this problem the following way.</p>
  
  <pre><code>SqlDataSource2.SelectParameters["getId"].DefaultValue = Convert.ToString(id);
  </code></pre>