title: Handling SqlCommand null parameters in a select query in asp.net
body: <p>I have this example code:</p>
  
  <pre><code>string query = "select * from xy where id == @id and name == @name";
  SqlCommand cmd = new SqlCommand(query);
  if(txtUsername.Text.Length &gt; 0)
  {
  cmd.Parameters.Add["@name", SqlDbType.VarChar);
  cmd.Parameters["@name"].Value = txtUsername.Text;
  }
  </code></pre>
  
  <p>Now if the txtUsername &lt;=0 I have to cut the query string dropping the AND and the name ==
   @name.
  How can I achieve this result?</p>
  
  <p>Thanks in advance.</p>