title: VB.NET DataSet Function
body: <p>I'm trying to see if there's a better way to use a function that returns a DataSet to fill a combo box, or a cleaner code, faster way, etc.</p>
  
  <p>Function:</p>
  
  <pre><code>    Public Function FillDataSet(ByVal dataSet As DataSet, ByVal queryString As String) As DataSet
        Using connection As New SqlConnection("Data Source=SQL;Initial Catalog=database; User ID=user;Password=password;")
           Using adapter As New SqlDataAdapter() With {.SelectCommand = New SqlCommand(queryString, connection)}
              adapter.Fill(DataSet)
           End Using
           Return DataSet
        End Using
      End Function
  </code></pre>
  
  <p>Calling Sub:</p>
  
  <pre><code>    Private Sub fillComboBox()
        comboBox.Items.Clear()
        Dim myDataSet As New DataSet
        myDataSet = FillDataSet(myDataSet , "SELECT rows FROM table")
        If myDataSet .Tables(0).Rows.Count &gt; 0 Then
          For Each row As DataRow In myDataSet .Tables(0).Rows
            comboBox.Items.Add(row(0))
          Next row
          comboBox.SelectedIndex = 0
        Else
          MsgBox("Empty table.", MsgBoxStyle.OkOnly, "Empty Table...")
        End If
        myDataSet .Dispose()
      End Sub
  </code></pre>