Two Datasets Same Query I have a form that allows the user to select a server.  This will execute the exact same query just on a different server.

private void QueryButton_Click(object sender, EventArgs e)
{
    switch (this.FindByComboBox.SelectedIndex)
    {
        case 0:
            ByTargetVDN();
            break;
        // ...
    }
}
void ByTargetVDN()
{
    int value = 0;
    if (this.ValueTextBox.Text.Length &gt; 0)
    {
        value = Convert.ToInt32(this.ValueTextBox.Text);
    }
    switch (this.ServerComboBox.SelectedIndex)
    {
        case 0:
            var gr01 = new GR01.TargetVDNDataTable();
            var adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
            if (value == 0)      
            {
                adapter.Fill(gr01);
            }
            else
            {
                adapter.FillByPeripheralNumber(gr01, value);
            }
            this.FindScriptsDataGrid.DataSource = gr01;
            break;
        case 1:
            var ds01 = new DS01.TargetVDNDataTable();
            var adapterNew = new ICMQuery.Models.DS01TableAdapters.TargetVDNTableAdapter();
            if (value == 0)
            {
                adapterNew.Fill(ds01);
            }
            else
            {
                adapterNew.FillByPeripheralNumber(ds01, value);
            }
            this.FindScriptsDataGrid.DataSource = ds01;
            break;
    }
}


There has got to be a way I can streamline this.  I was hoping for something along the lines of 

switch (this.ServerComboBox.SelectedIndex)
  {
    case 0:
      var ds = new GR01.TargetVDNDataTable();
      var adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
      break;
    case 1:
      ds = new GR01.TargetVDNDataTable();
      adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
      break;
  }
if (value == 0)
  {
    adapter.Fill(ds);
  }
else
  {
    adapter.FillByPeripheralNumber(ds, value);
  }
this.FindScriptsDataGrid.DataSource = ds;


but of course this doesn't work.
if (this.ValueTextBox.Text.Length &gt; 0)
{
    value = Convert.ToInt32(this.ValueTextBox.Text);
}


This will throw a nullpointerexception if Text is null, use TryParse instead

int value;
bool result = Int32.TryParse(ValueTextBox.Text, out value);
if(result){
 // use value
}


I don't recommend you using SelectedIndex, because it breaks your code if the ordering get changed in the future, I would use SelectedItem instead.

Notice that you are repeating code, which is not a brilliant idea.

TheTypeForThis gr01 = null;
TheTypeForThis adapter = null;
switch (this.ServerComboBox.SelectedIndex)
{
    case 0:
        gr01 = new GR01.TargetVDNDataTable();
        adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
        break;
    case 1:
        var ds01 = new DS01.TargetVDNDataTable();
        var adapterNew = new ICMQuery.Models.DS01TableAdapters.TargetVDNTableAdapter();
       break;
}

 if (value == 0)      
 {
    adapter.Fill(gr01);
 }
 else
 {
    adapter.FillByPeripheralNumber(gr01, value);
 }
 this.FindScriptsDataGrid.DataSource = gr01;