title: Optimizing C# code / hard coding
body: <p>First, I want to mention that I am not a coder.  Basically, I got stuck learning C# from scratch, which I don't really mind.</p>
  
  <p>Here is a little background on what my code is trying to accomplish.</p>
  
  <p>I have a cascading DDL1 for database name.  When a user selects the database name,they would need to select DDL2 for the date range (which runs different queries with "some" parameters).</p>
  
  <p>The query that it runs involves joining 2 tables that are from different databases (i think this is the overall issue that i'm having).</p>
  
  <p>I can get the code to work, but it seems really tedious and i'm sure that hard coding is frowned upon, unless necessary.  I did a bit of research, and it looks like I would need to use dynamic SQL to parameterized the database name in a stored procedure, which I prefer not to use.</p>
  
  <p>So, in summary.  For each database name, and each date range, I would need to hard code them separately.  That would be a total of 4 blocks of code for each database.  If the data all existed in 1 table, I wouldn't have this issue.</p>
  
  <p>Again, I'm fairly new and pretty much a beginner.  Please let me know if its possible to optimize the code and make it more effcient, because as of right now, it looks like bad coding to me.  </p>
  
  <p>The code is provided below, thanks:</p>
  
  <pre><code>protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
  
  {
      DataTable dt = new DataTable();
      SqlDataAdapter Adpt;
  
      if (DropDownList1.SelectedValue == string.Empty &amp; DropDownList2.SelectedValue == string.Empty)
      {
      }
  
      if (DropDownList1.SelectedItem.Text == "ytd" &amp; DropDownList2.SelectedValue == "db1")
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db1ConnectionString"].ConnectionString))
          {
              SqlCommand cmd = new SqlCommand("select sum(column1) from table1 inner join db2.dbo.table2 on db2.dbo.table2.ID = db1.dbo.table3.id where somecolumn = @somecolumn and Date &lt;= GetDate() AND YEAR(Date) = year(GetDate()) AND Status = @Status", con);
              cmd.Parameters.AddWithValue("@somecolumn", DropDownList2.SelectedValue);
              cmd.Parameters.AddWithValue("@status", "done");
              Adpt = new SqlDataAdapter(cmd);
              new SqlDataAdapter(cmd).Fill(dt);
          }
  
      if (DropDownList1.SelectedItem.Text == "yesterday" &amp; DropDownList2.SelectedValue == "db1")
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db1ConnectionString"].ConnectionString))
          {
              SqlCommand cmd = new SqlCommand("select sum(column1) from table1 inner join db2.dbo.table2 on db2.dbo.table2.ID = db1.dbo.table3.id where somecolumn = @somecolumn and Date &gt;= DATEADD(DAY, DATEDIFF(DAY, 1, GETDATE()), 0) AND Date &lt;= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) AND Status = @Status", con);
              cmd.Parameters.AddWithValue("@somecolumn", DropDownList2.SelectedValue);
              cmd.Parameters.AddWithValue("@status", "done");
              Adpt = new SqlDataAdapter(cmd);
              new SqlDataAdapter(cmd).Fill(dt);
  
      //if same code, different database
          {
          }
  
      //if same code, different database
          {
          }
  
      //else if same code, different database           
          {
          }
  
      GridView1.DataSource = dt;
      GridView1.DataBind();
  
  }
  
  
  protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
  {
      {
          if (DropDownList2.SelectedIndex == 0)
          {
              DropDownList1.SelectedIndex = 0;
              DropDownList1.Enabled = false;
          }
          else
          {
              DropDownList1.Enabled = true;
              DropDownList1.SelectedIndex = 0;
          }
  
      }
  }
  </code></pre>
  
  <p>}</p>