title: How to Reuse Logic Without Copy/Pasting Blocks of Code - C#
body: <p>My project reuses the code below multiple times with only a few variables changing. I've noted the values that change with e.g. ***value***.</p>
  
  <pre><code>  if (comboBox1.Text == ***"Most recent first"***)
              {
                  string dgvconn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\n0740572\Projects\CW\CW\bin\Debug\Database1.mdf;Integrated Security=True";
                  string sql = "select * from Records where UserID = @userID Order By ***Date Desc***";
                  SqlConnection connection = new SqlConnection(dgvconn);
                  SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
                  dataadapter.SelectCommand.Parameters.AddWithValue("@userID", currentUserID);
                  DataSet ds = new DataSet();
                  connection.Open();
                  dataadapter.Fill(ds, "Records");
                  connection.Close();
                  recordsDataGridView.DataSource = ds;
                  recordsDataGridView.DataMember = "Records";
              }
  </code></pre>
  
  <p>How can I use this same logic, with different values, without copying and pasting the if statement multiple times?</p>