title: Properly Loading DataGridView From DataReader
body: <p>Reading <a href="http://msdn.microsoft.com/en-us/library/at23yw2h%28vs.80%29.aspx" rel="nofollow">this</a> documentation I was unde the impression that the following code would work: </p>
  
  <pre><code>    using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
      {
          try
          {
              using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
              {
                  cmd.CommandType = CommandType.StoredProcedure;
                  string SearchString = textBoxSearchSting.Text;
                  cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;
  
                  con.Open();
  
                  dataGridView1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
              }
          }
          catch (Exception) { throw; }
          finally { con.Close(); }
      }
  </code></pre>
  
  <p>Or that I could atleast use the following: </p>
  
  <pre><code>        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
          {
              try
              {
                  using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
                  {
                      cmd.CommandType = CommandType.StoredProcedure;
                      string SearchString = textBoxSearchSting.Text;
                      cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;
  
                      con.Open();
  
                      SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                      dataGridView1.DataSource = reader;
                  }
              }
              catch (Exception) { throw; }
              finally { con.Close(); }
          }
  </code></pre>
  
  <p>but niether of the above work. Using the below code does work, with the same search value, is the below the best way to fill my datagridview with my query though? And why won't either of the above work?</p>
  
  <pre><code>        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
          {
              try
              {
                  using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
                  {
                      cmd.CommandType = CommandType.StoredProcedure;
                      string SearchString = textBoxSearchSting.Text;
                      cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;
  
                      con.Open();                           
  
                      DataTable dt = new DataTable();
                      dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
  
                      if (dt.Rows.Count &gt; 0 &amp;&amp; dt != null)
                      {
                          dataGridView1.DataSource = dt;
                      }
                  }
              }
              catch (Exception) { throw; }
              finally { con.Close(); }
          }
  </code></pre>
  
  <p>Below also works, would I be better of using a data adapter, for any reason?</p>
  
  <pre><code>        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.C_Str))
          {
              try
              {
                  using (SqlCommand cmd = new SqlCommand("PerformSearch", con))
                  {
                      cmd.CommandType = CommandType.StoredProcedure;
                      string SearchString = textBoxSearchSting.Text;
                      cmd.Parameters.Add("SearchString", SqlDbType.VarChar).Value = SearchString;
  
                      DataTable dt = new DataTable();
  
                      using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                      {
                          con.Open();
  
                          da.Fill(dt);
  
                          if (dt.Rows.Count &gt; 0 &amp;&amp; dt != null)
                          {
                              dataGridView1.DataSource = dt;
                          }
                      }
                  }
              }
              catch (Exception) { throw; }
              finally { con.Close(); }
          }
  </code></pre>