title: VB.net - Dynamic database connection combobox
body: <p>I have some code which populates a combobox with database names. I then have a button which saves the name back to My.Settings and creates the connection string. </p>
  
  <p>This is working for the most part. If i select a database it will display the correct information for that database in my form, then if i go back out to my database selector and do the same it will still display the correct settings.</p>
  
  <p>The problem is, i have a reload datagridview sub which re-populates the grid. If i select a database and then drop back out and pick another and then run the reload sub it pulls the settings from the other database. Even though when it initially launches it has the correct database information in the form.</p>
  
  <p>I've tried My.Settings.Reset and My.Settings.Save to no avail.</p>
  
  <p>Here's a snippet of code:</p>
  
  <pre><code>Dim strDatabase As String = My.Settings.Database
  Dim strDatabaseCon As String = My.Settings.DatabaseCon
  
  Public Sub Loadcombobox()
      My.Settings.Reset()
      My.Settings.Save()
      Try
  
          Dim con As New SqlConnection(" Data Source=******\SQLEXPRESS; Initial Catalog =" &amp; strDatabase &amp; "; Integrated Security=true")
          con.Open()
          Dim cmd As New SqlCommand("Select * From sys.databases Where database_id &gt; 4", con)
          Dim read As SqlDataReader = cmd.ExecuteReader()
          While read.Read()
              ComboBox1.Items.Add(read.Item(0))
          End While
          con.Close()
      Catch ex As Exception
          MessageBox.Show(ex.Message)
      End Try
  End Sub
  
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
      If ComboBox1.Text = "" Then
          MessageBox.Show("Please select a customer")
      Else
  
  
          My.Settings.Database = ComboBox1.Text
  
          strDatabase = My.Settings.Database
  
          My.Settings.DatabaseCon = "Data Source=******\SQLEXPRESS; Initial Catalog =" &amp; strDatabase &amp; "; Integrated Security=true"
  
          'Here we redeclare the strDatabaseCon = My.Settings.DatabaseCon
          strDatabaseCon = My.Settings.DatabaseCon
  
          My.Settings.Save()
  
          Mainform.Show()
          Me.Hide()
      End If
  End Sub
  </code></pre>
  
  <p>This sub is on a different form and causes the datagridview to populate from the first database.</p>
  
  <pre><code>Sub ReloadRecords()
      Try
          FillDataGrid("SELECT * FROM Change_Control order by [DateOfChange] desc", dgvData)
          dgvData.Columns(0).Visible = False
          dgvData.Columns(1).Width = 200 ''Date of Change''
          dgvData.Columns(1).HeaderCell.Value = "Date Of Change"
          dgvData.Columns(2).Width = 200 ''Name''
          dgvData.Columns(2).HeaderCell.Value = "Who Made the Change"
          dgvData.Columns(3).Width = 470 ''Change made''
          dgvData.Columns(3).HeaderCell.Value = "Change Made"
          dgvData.Columns(4).Visible = False
      Catch ex As Exception
          MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
      End Try
  
  End Sub
  </code></pre>
  
  <p>This is the FillDataGrid Function</p>
  
  <pre><code>Dim strDatabaseCon As String = My.Settings.DatabaseCon
  Public Function FillDataGrid(ByVal Sqlstring As String, ByVal MyDataGrid As DataGridView)
      Dim SQLCon As New SQLConnection(strDatabaseCon)
      Dim SQLAdapter As New SqlDataAdapter()
      Dim myDataset As New DataSet()
  
      SQLCon.Open()
  
      Try
          SQLAdapter.SelectCommand = New SqlCommand(Sqlstring, SQLCon)
          SQLAdapter.Fill(myDataset)
          MyDataGrid.DataSource = myDataset.Tables(0)
      Catch ex As Exception
      End Try
  
      SQLCon.Close()
      SQLAdapter.Dispose()
      myDataset.Dispose()
      Return True
  End Function
  </code></pre>